Sage-Bionetworks / sage-monorepo

Where OpenChallenges, Schematic, and other Sage open source apps are built
https://sage-bionetworks.github.io/sage-monorepo/
Apache License 2.0
23 stars 12 forks source link

Develop error handling for microservices #978

Closed tschaffter closed 1 year ago

tschaffter commented 1 year ago

We have a schema for the error but the microservices are not implementing it. This schema was derived from RFC 7807.

Since then we adopted a microservice architecture. One of the error design proposed here is to add a global error code to the error object that enables to identify the microservice that reported the issue.

One important piece of information missing from our error schema is a timestamp.

Two fields that should be defined together in the RFC 7807 are the title and status as highlighted in the following Java example from zalando/problem-spring-web:

       return Problem.builder()
                      .withTitle(status.getReasonPhrase())
                      .withStatus(status)
                      .withDetail(exception.getMessage())
                      .with("parameter", exception.getParameterName());

Here are error schemas used by GitHub:

    basic-error:
      title: Basic Error
      description: Basic Error
      type: object
      properties:
        message:
          type: string
        documentation_url:
          type: string
        url:
          type: string
        status:
          type: string
    validation-error-simple:
      title: Validation Error Simple
      description: Validation Error Simple
      type: object
      required:
      - message
      - documentation_url
      properties:
        message:
          type: string
        documentation_url:
          type: string
        errors:
          type: array
          items:
            type: string

Questions

tschaffter commented 1 year ago

This is our current schemas:

  title:
    type: string
    description: A human readable documentation for the problem type
  status:
    type: integer
    description: The HTTP status code
  detail:
    type: string
    description: A human readable explanation specific to this occurrence of
      the problem
  type:
    type: string
    description: An absolute URI that identifies the problem type

Notes:

tschaffter commented 1 year ago

~Spring Boost~ OpenAPI generator does not seem to support the MIME type application/problem+json?

  default ResponseEntity<OrganizationDto> getOrganization(Long organizationId) {
    getRequest()
        .ifPresent(
            request -> {
              for (MediaType mediaType : MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                  String exampleString = "{ \"name\" : \"DREAM\" }";
                  ApiUtil.setExampleResponse(request, "application/json", exampleString);
                  break;
                }
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/problem+json"))) {
                  String exampleString =
                      "Custom MIME type example not yet supported: application/problem+json";
                  ApiUtil.setExampleResponse(request, "application/problem+json", exampleString);
                  break;
                }
              }
            });
    return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
  }