View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.llm;
17  
18  import org.codelibs.fess.exception.FessSystemException;
19  
20  /**
21   * Exception thrown when an error occurs during LLM operations.
22   *
23   * @author FessProject
24   */
25  public class LlmException extends FessSystemException {
26  
27      private static final long serialVersionUID = 1L;
28  
29      /** Error code for rate limit (HTTP 429). */
30      public static final String ERROR_RATE_LIMIT = "rate_limit";
31  
32      /** Error code for authentication failure (HTTP 401/403). */
33      public static final String ERROR_AUTH = "auth_error";
34  
35      /** Error code for service unavailable (HTTP 502/503). */
36      public static final String ERROR_SERVICE_UNAVAILABLE = "service_unavailable";
37  
38      /** Error code for request timeout. */
39      public static final String ERROR_TIMEOUT = "timeout";
40  
41      /** Error code for context length exceeded. */
42      public static final String ERROR_CONTEXT_LENGTH_EXCEEDED = "context_length_exceeded";
43  
44      /** Error code for model not found. */
45      public static final String ERROR_MODEL_NOT_FOUND = "model_not_found";
46  
47      /** Error code for invalid response from the LLM provider. */
48      public static final String ERROR_INVALID_RESPONSE = "invalid_response";
49  
50      /** Error code for connection errors. */
51      public static final String ERROR_CONNECTION = "connection_error";
52  
53      /** Error code for unknown errors. */
54      public static final String ERROR_UNKNOWN = "unknown";
55  
56      /** The error code indicating the type of LLM error. */
57      private final String errorCode;
58  
59      /**
60       * Creates a new exception with the specified message.
61       *
62       * @param message the error message
63       */
64      public LlmException(final String message) {
65          super(message);
66          errorCode = ERROR_UNKNOWN;
67      }
68  
69      /**
70       * Creates a new exception with the specified message and cause.
71       *
72       * @param message the error message
73       * @param cause the cause of the exception
74       */
75      public LlmException(final String message, final Throwable cause) {
76          super(message, cause);
77          errorCode = ERROR_UNKNOWN;
78      }
79  
80      /**
81       * Creates a new exception with the specified message and error code.
82       *
83       * @param message the error message
84       * @param errorCode the error code
85       */
86      public LlmException(final String message, final String errorCode) {
87          super(message);
88          this.errorCode = errorCode;
89      }
90  
91      /**
92       * Creates a new exception with the specified message, error code and cause.
93       *
94       * @param message the error message
95       * @param errorCode the error code
96       * @param cause the cause of the exception
97       */
98      public LlmException(final String message, final String errorCode, final Throwable cause) {
99          super(message, cause);
100         this.errorCode = errorCode;
101     }
102 
103     /**
104      * Gets the error code.
105      *
106      * @return the error code
107      */
108     public String getErrorCode() {
109         return errorCode;
110     }
111 }