Skip to content

Commit 7cf67aa

Browse files
rlazodaymxn
andauthored
Make FirebaseVertexAIException abstract, and constructors internal (#6368)
For better API evolution while maintaining backward compatibility, moved `FirebaseVertexAIException` from sealed to abstract class. Also, since the dev shoul not be creating instances of the class, or its subclasses, marking those constructors as internal. --------- Co-authored-by: Daymon <[email protected]>
1 parent 065c732 commit 7cf67aa

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

firebase-vertexai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
* [changed] **Breaking Change**: Migrated `FirebaseVertexAIException` from a sealed class to an abstract class, and marked constructors as internal. (#6368)
23
* [feature] Added support for `title` and `publicationDate` in citations. (#6309)
34
* [feature] Added support for `frequencyPenalty`, `presencePenalty`, and `HarmBlockMethod`. (#6309)
45
* [changed] **Breaking Change**: Introduced `Citations` class. Now `CitationMetadata` wraps that type. (#6276)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Exceptions.kt

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import com.google.firebase.vertexai.internal.util.toPublic
2222
import kotlinx.coroutines.TimeoutCancellationException
2323

2424
/** Parent class for any errors that occur from the [FirebaseVertexAI] SDK. */
25-
public sealed class FirebaseVertexAIException(message: String, cause: Throwable? = null) :
26-
RuntimeException(message, cause) {
25+
public abstract class FirebaseVertexAIException
26+
internal constructor(message: String, cause: Throwable? = null) : RuntimeException(message, cause) {
2727

2828
internal companion object {
2929

@@ -68,29 +68,29 @@ public sealed class FirebaseVertexAIException(message: String, cause: Throwable?
6868
}
6969

7070
/** Something went wrong while trying to deserialize a response from the server. */
71-
public class SerializationException(message: String, cause: Throwable? = null) :
71+
public class SerializationException
72+
internal constructor(message: String, cause: Throwable? = null) :
7273
FirebaseVertexAIException(message, cause)
7374

7475
/** The server responded with a non 200 response code. */
75-
public class ServerException(message: String, cause: Throwable? = null) :
76+
public class ServerException internal constructor(message: String, cause: Throwable? = null) :
7677
FirebaseVertexAIException(message, cause)
7778

78-
/** The server responded that the API Key is not valid. */
79-
public class InvalidAPIKeyException(message: String, cause: Throwable? = null) :
79+
/** The provided API Key is not valid. */
80+
public class InvalidAPIKeyException
81+
internal constructor(message: String, cause: Throwable? = null) :
8082
FirebaseVertexAIException(message, cause)
8183

8284
/**
83-
* A request was blocked for some reason.
85+
* A request was blocked.
8486
*
8587
* See the [response's][response] `promptFeedback.blockReason` for more information.
8688
*
87-
* @property response the full server response for the request.
89+
* @property response The full server response.
8890
*/
8991
// TODO(rlazo): Add secondary constructor to pass through the message?
90-
public class PromptBlockedException(
91-
public val response: GenerateContentResponse,
92-
cause: Throwable? = null
93-
) :
92+
public class PromptBlockedException
93+
internal constructor(public val response: GenerateContentResponse, cause: Throwable? = null) :
9494
FirebaseVertexAIException(
9595
"Prompt was blocked: ${response.promptFeedback?.blockReason?.name}",
9696
cause,
@@ -104,26 +104,24 @@ public class PromptBlockedException(
104104
* (countries and territories) where the API is available.
105105
*/
106106
// TODO(rlazo): Add secondary constructor to pass through the message?
107-
public class UnsupportedUserLocationException(cause: Throwable? = null) :
107+
public class UnsupportedUserLocationException internal constructor(cause: Throwable? = null) :
108108
FirebaseVertexAIException("User location is not supported for the API use.", cause)
109109

110110
/**
111111
* Some form of state occurred that shouldn't have.
112112
*
113113
* Usually indicative of consumer error.
114114
*/
115-
public class InvalidStateException(message: String, cause: Throwable? = null) :
115+
public class InvalidStateException internal constructor(message: String, cause: Throwable? = null) :
116116
FirebaseVertexAIException(message, cause)
117117

118118
/**
119119
* A request was stopped during generation for some reason.
120120
*
121-
* @property response the full server response for the request
121+
* @property response The full server response.
122122
*/
123-
public class ResponseStoppedException(
124-
public val response: GenerateContentResponse,
125-
cause: Throwable? = null
126-
) :
123+
public class ResponseStoppedException
124+
internal constructor(public val response: GenerateContentResponse, cause: Throwable? = null) :
127125
FirebaseVertexAIException(
128126
"Content generation stopped. Reason: ${response.candidates.first().finishReason?.name}",
129127
cause,
@@ -134,7 +132,8 @@ public class ResponseStoppedException(
134132
*
135133
* Usually occurs due to a user specified [timeout][RequestOptions.timeout].
136134
*/
137-
public class RequestTimeoutException(message: String, cause: Throwable? = null) :
135+
public class RequestTimeoutException
136+
internal constructor(message: String, cause: Throwable? = null) :
138137
FirebaseVertexAIException(message, cause)
139138

140139
/**
@@ -143,17 +142,19 @@ public class RequestTimeoutException(message: String, cause: Throwable? = null)
143142
* For a list of valid locations, see
144143
* [Vertex AI locations.](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations#available-regions)
145144
*/
146-
public class InvalidLocationException(location: String, cause: Throwable? = null) :
145+
public class InvalidLocationException
146+
internal constructor(location: String, cause: Throwable? = null) :
147147
FirebaseVertexAIException("Invalid location \"${location}\"", cause)
148148

149149
/**
150150
* The service is not enabled for this Firebase project. Learn how to enable the required services
151151
* in the
152152
* [Firebase documentation.](https://firebase.google.com/docs/vertex-ai/faq-and-troubleshooting#required-apis)
153153
*/
154-
public class ServiceDisabledException(message: String, cause: Throwable? = null) :
154+
public class ServiceDisabledException
155+
internal constructor(message: String, cause: Throwable? = null) :
155156
FirebaseVertexAIException(message, cause)
156157

157158
/** Catch all case for exceptions not explicitly expected. */
158-
public class UnknownException(message: String, cause: Throwable? = null) :
159+
public class UnknownException internal constructor(message: String, cause: Throwable? = null) :
159160
FirebaseVertexAIException(message, cause)

0 commit comments

Comments
 (0)