Skip to content

Commit

Permalink
Add YAML loading for error template to Error Objects V2 (#1316)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Morton <aaron.morton@datastax.com>
  • Loading branch information
Hazel-Datastax and amorton authored Aug 15, 2024
1 parent bd0028a commit 0ebe9e3
Show file tree
Hide file tree
Showing 23 changed files with 1,278 additions and 130 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
Expand Down Expand Up @@ -128,6 +135,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.12.0</version>
</dependency>
<dependency>
<groupId>com.bpodgursky</groupId>
<artifactId>jbool_expressions</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* ErrorCode} that is unique within the family and scope.
*
* <p>To facilitate better error messages we template the messages in a {@link ErrorTemplate} that
* is loaded from a properties file. The message for the error may change with each instance of the
* is loaded from a properties file. The body for the error may change with each instance of the
* exception, for example to include the number of filters that were included in a request.
*
* <p>The process of adding a new Error Code is:
Expand All @@ -25,8 +25,8 @@
* it does not then use {@link ErrorScope#NONE}.
* <li>Decide on the {@link ErrorCode}, it should be unique within the Family and Scope
* combination.
* <li>Add the error to file read by {@link ErrorTemplate} to define the title and templated
* message body.
* <li>Add the error to file read by {@link ErrorTemplate} to define the title and templated body
* body.
* <li>Add the error code to the Code enum for the Exception class, such as {@link
* FilterException.Code} or {@link RequestException.Code} with the same name. When the enum is
* instantiated at JVM start the error template is loaded.
Expand Down Expand Up @@ -60,44 +60,51 @@ public abstract class APIException extends RuntimeException
public final String title;

/**
* Message for this instance of the error.
* Message body for this instance of the error.
*
* <p>Messages may be unique for each instance of the error code, they are typically driven from
* the {@link ErrorTemplate}.
*
* <p>This is the processed message to be returned to the client.
* <p>This is the processed body to be returned to the client. NOT called body to avoid confusion
* with getMessage() on the RuntimeException
*/
public final String message;
public final String body;

public APIException(ErrorInstance errorInstance) {
this.errorId = errorInstance.errorId();
this.family = errorInstance.family();
this.scope = errorInstance.scope().safeScope();
this.scope = errorInstance.scope().scope();
this.code = errorInstance.code();
this.title = errorInstance.title();
this.message = errorInstance.message();
this.body = errorInstance.body();
}

public APIException(
ErrorFamily family, ErrorScope scope, String code, String title, String message) {
this(new ErrorInstance(UUID.randomUUID(), family, scope, code, title, message));
ErrorFamily family, ErrorScope scope, String code, String title, String body) {
this(new ErrorInstance(UUID.randomUUID(), family, scope, code, title, body));
}

@Override
public CommandResponseError get() {
return null;
}

/**
* Overrides to return the {@link #body} of the error. Using the body as this is effectively the
* message, the structure we want to return to the in the API is the {@link CommandResponseError}
* from {@link #get()}
*
* @return
*/
@Override
public String getMessage() {
// TODO: work out the message, is it just the message or a formatted string of all the
// properties ?
return super.getMessage();
return body;
}

@Override
public String toString() {
// TODO: make a nice string for error logging etc.
return super.toString();
return String.format(
"%s{errorId=%s, family=%s, scope='%s', code='%s', title='%s', body='%s'}",
getClass().getSimpleName(), errorId, family, scope, code, title, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.stargate.sgv2.jsonapi.exception.playing;

public class EmbeddingProviderException extends ServerException {

public static final Scope SCOPE = Scope.EMBEDDING_PROVIDER;

public EmbeddingProviderException(ErrorInstance errorInstance) {
super(errorInstance);
}

public enum Code implements ErrorCode<EmbeddingProviderException> {
CLIENT_ERROR,
SERVER_ERROR;

private final ErrorTemplate<EmbeddingProviderException> template;

Code() {
template = ErrorTemplate.load(EmbeddingProviderException.class, FAMILY, SCOPE, name());
}

@Override
public ErrorTemplate<EmbeddingProviderException> template() {
return template;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface ErrorCode<T extends APIException> {

/**
* Gets an instance of the {@link APIException} the error code represents without providing any
* substitution values for the error message.
* substitution values for the error body.
*
* @return Instance of {@link APIException} the error code represents.
*/
Expand All @@ -37,10 +37,10 @@ default T get() {

/**
* Gets an instance of the {@link APIException} the error code represents, providing substitution
* values for the error message as a param array.
* values for the error body as a param array.
*
* @param values Substitution values for the error message. The array length must be a multiple of
* 2, each pair of strings is treated as a key-value pair for example ["key-1", "value-1",
* @param values Substitution values for the error body. The array length must be a multiple of 2,
* each pair of strings is treated as a key-value pair for example ["key-1", "value-1",
* "key-2", "value-2"]
* @return Instance of {@link APIException} the error code represents.
*/
Expand All @@ -56,9 +56,9 @@ default T get(String... values) {

/**
* Gets an instance of the {@link APIException} the error code represents, providing substitution
* values for the error message as a param array.
* values for the error body as a param array.
*
* @param values May of substitution values for the error message.
* @param values May of substitution values for the error body.
* @return Instance of {@link APIException} the error code represents.
*/
default T get(Map<String, String> values) {
Expand Down
Loading

0 comments on commit 0ebe9e3

Please sign in to comment.