Skip to content

Commit

Permalink
chore: using ProblemError defined in selfcare-common (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuraf authored Aug 23, 2024
1 parent 9d5839e commit c76a6ed
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 32 deletions.
79 changes: 74 additions & 5 deletions apps/user-ms/src/main/docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -825,17 +825,48 @@
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"204" : {
"description" : "No Content"
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/json" : { }
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"401" : {
"description" : "Not Authorized"
"description" : "Not Authorized",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"403" : {
"description" : "Not Allowed"
"description" : "Forbidden",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"404" : {
"description" : "Not Found",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
}
},
"security" : [ {
Expand Down Expand Up @@ -1231,6 +1262,17 @@
"enum" : [ "ROOT", "DEV", "COLL", "PROD" ],
"type" : "string"
},
"InvalidParam" : {
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
},
"reason" : {
"type" : "string"
}
}
},
"LocalDateTime" : {
"format" : "date-time",
"type" : "string",
Expand Down Expand Up @@ -1316,6 +1358,33 @@
"enum" : [ "ADMIN", "ANY" ],
"type" : "string"
},
"Problem" : {
"type" : "object",
"properties" : {
"detail" : {
"type" : "string"
},
"instance" : {
"type" : "string"
},
"invalidParams" : {
"type" : "array",
"items" : {
"$ref" : "#/components/schemas/InvalidParam"
}
},
"status" : {
"format" : "int32",
"type" : "integer"
},
"title" : {
"type" : "string"
},
"type" : {
"type" : "string"
}
}
},
"Product" : {
"required" : [ "productId", "role", "productRoles" ],
"type" : "object",
Expand Down
51 changes: 47 additions & 4 deletions apps/user-ms/src/main/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -577,14 +577,32 @@ paths:
schema:
$ref: "#/components/schemas/OnboardedProductState"
responses:
"200":
description: OK
"204":
description: No Content
"400":
description: Bad Request
content:
application/json: {}
application/json:
schema:
$ref: "#/components/schemas/Problem"
"401":
description: Not Authorized
content:
application/json:
schema:
$ref: "#/components/schemas/Problem"
"403":
description: Not Allowed
description: Forbidden
content:
application/json:
schema:
$ref: "#/components/schemas/Problem"
"404":
description: Not Found
content:
application/json:
schema:
$ref: "#/components/schemas/Problem"
security:
- SecurityScheme: []
/users/{id}/user-registry:
Expand Down Expand Up @@ -871,6 +889,13 @@ components:
- COLL
- PROD
type: string
InvalidParam:
type: object
properties:
name:
type: string
reason:
type: string
LocalDateTime:
format: date-time
type: string
Expand Down Expand Up @@ -943,6 +968,24 @@ components:
- ADMIN
- ANY
type: string
Problem:
type: object
properties:
detail:
type: string
instance:
type: string
invalidParams:
type: array
items:
$ref: "#/components/schemas/InvalidParam"
status:
format: int32
type: integer
title:
type: string
type:
type: string
Product:
required:
- productId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import it.pagopa.selfcare.user.mapper.UserMapper;
import it.pagopa.selfcare.user.model.LoggedUser;
import it.pagopa.selfcare.user.model.UpdateUserRequest;
import it.pagopa.selfcare.user.controller.response.UserInstitutionWithActions;
import it.pagopa.selfcare.user.model.constants.OnboardedProductState;
import it.pagopa.selfcare.user.service.UserRegistryService;
import it.pagopa.selfcare.user.service.UserService;
Expand All @@ -30,11 +29,14 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.jboss.resteasy.reactive.ResponseStatus;
import org.openapi.quarkus.user_registry_json.model.Problem;

import java.util.List;

Expand Down Expand Up @@ -153,6 +155,13 @@ public Uni<Void> deleteProducts(@PathParam(value = "userId") String userId,
* @return A uni&lt;response&gt;
*/
@Operation(summary = "Update user status with optional filter for institution, product, role and productRole")
@APIResponses(value = {
@APIResponse(responseCode = "204", description = "No Content"),
@APIResponse(responseCode = "400", description = "Bad Request", content = @Content(schema = @Schema(implementation = Problem.class))),
@APIResponse(responseCode = "401", description = "Not Authorized", content = @Content(schema = @Schema(implementation = Problem.class))),
@APIResponse(responseCode = "403", description = "Forbidden", content = @Content(schema = @Schema(implementation = Problem.class))),
@APIResponse(responseCode = "404", description = "Not Found", content = @Content(schema = @Schema(implementation = Problem.class)))
})
@PUT
@Path(value = "/{id}/status")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@

import it.pagopa.selfcare.user.exception.InvalidRequestException;
import it.pagopa.selfcare.user.exception.ResourceNotFoundException;
import it.pagopa.selfcare.user.model.error.Problem;
import it.pagopa.selfcare.user.model.error.ProblemError;
import jakarta.ws.rs.core.Response;
import org.apache.http.HttpStatus;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.openapi.quarkus.user_registry_json.model.Problem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


public class ExceptionHandler {

Expand All @@ -35,23 +31,7 @@ public RestResponse<String> toResponse(Exception exception) {
@ServerExceptionMapper
public Response toResponse(ResourceNotFoundException exception) {
LOGGER.error("{}: {}", SOMETHING_HAS_GONE_WRONG_IN_THE_SERVER, exception.getMessage());
Problem problem = createProblem(exception.getMessage(), HttpStatus.SC_NOT_FOUND, exception.getCode());
Problem problem = new Problem(exception.getMessage(), null, null, HttpStatus.SC_NOT_FOUND, exception.getMessage(), null);
return Response.status(Response.Status.NOT_FOUND).entity(problem).build();
}

private Problem createProblem(String errorMessage, Integer status, String code) {
Problem problem = new Problem();
problem.setStatus(status);
problem.setErrors(createProblemError(errorMessage,code));
return problem;
}

private List<ProblemError> createProblemError(String message, String code) {
List<ProblemError> list = new ArrayList<>();
list.add(ProblemError.builder()
.code(code)
.detail(message)
.build());
return list;
}
}

0 comments on commit c76a6ed

Please sign in to comment.