https://www.boundaryml.com logo
🟡 Error handling for REST API
# questions
t
Hey, how to do error handling for REST api ? https://docs.boundaryml.com/guide/baml-basics/error-handling
v
@baml-sam do you have ideas here? I think we throw HTTP error codes for anything that doesn't parse
b
Agh... looks like the error docs for that API got lost during some refactor. We don't even have a redirect on the link that we put in the errors 🙁
But yes, we always return some HTTP error code. Looking through them, the error codes aren't quite the most useful thing though... What I would instead rely on is that the HTTP body in event of an error is guaranteed to be well-formed JSON, with
error: invalid_argument | client_error | validation_failure | finish_reason_error | client_http_error | internal_error
t
Yup i am parsing it like this only, but it would be great if generator can provide this as well, for any future changes support.
b
If the generators could support this? Can you clarify?
t
So i think baml java client should own below classes
Copy code
@Data
public class AIException extends RuntimeException {

  private final HttpStatus httpStatus;
  private final AIServiceError error;

  public AIException(ApiException e) {
    super(e.getMessage());
    this.httpStatus = HttpStatus.valueOf(e.getCode());
    this.error = ObjectMapperUtility.readValue(e.getResponseBody(), AIServiceError.class);
  }

}

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class AIServiceError {

  private String error;
  private String prompt;

  @JsonProperty("raw_output")
  private String rawOutput;

  private String message;

  @Getter
  @RequiredArgsConstructor
  public enum ErrorType {
    VALIDATION_FAILURE("validation_failure");
    private final String value;

    public static ErrorType fromValue(String value) {
      return Arrays.stream(ErrorType.values()).filter(errorType -> errorType.value.equals(value))
          .findAny().orElse(null);
    }
  }

}