-2

i have a rest api:

@RestController

.....

@PostMapping(value = "/v1/ms/prop")
    public @ResponseBody ResponseEntity<Object> orch(@RequestBody @Valid Request request){
        
            
        

Response response = serv.prop(request);
            return ResponseEntity.ok(response);

and serv.prop(request):

Response response = new Response ();
    
    try {
        calculate(**data**);
        callExternalService();
    } catch (Exception e) {

   **//intercept status error code**
   **// writeError(data,errorStatusCode)**
        
    } finally {
        writeAuditOperation(auditLog);
        MDC.clear();
    }

Inside the catch, i want, ONLY IN this api rest, intercept the type of error (400,404,500,502 ecc ecc ) and call an external service that log this error.

Thanks for the help

5
  • You can able to get details of HTTP status code if your calculate(); or callExternalService(); throws error with HTTP status code. Share details about what exceptions being thrown from these methods. Commented May 16, 2022 at 14:29
  • i have update my post Commented May 16, 2022 at 14:57
  • I mean, do you know what type of exceptions being thrown from calculate() or callExternalService() Commented May 16, 2022 at 14:59
  • calculate -> 5XX, callExternalService() -> 4XX Commented May 16, 2022 at 15:14
  • stackoverflow.com/questions/37302615/… let us know if this helps, if not, will put some answer. Commented May 16, 2022 at 16:08

1 Answer 1

1

Rather than manually handling Exceptions in your controller, you should try @ExceptionHandler.

Check: https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

Sign up to request clarification or add additional context in comments.

2 Comments

my controller has 3 postmapping methods. How can i do for apply at only one of these methods? And in case of error, i call, inside the service, an external service for comunicate the type of error and other data that are presents ONLY inside the service (not the controller)
Maybe you should re-consider your general application architecture. Instead of trying to intercept status codes from the call to external services, try to translate their responses to meaningful exceptions in you applications and pick them in you catch blocks (instead of general Exception type).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.