Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Case Study :: Exception Handling

Technology - Spring Boot Tracing Difficulty - Easy Implementation Difficulty - Easy

The Problem

If unhandled error is thrown, there is probability that the error could cause chaos, like blank page or force close.

The Objective

Fix the error happened with exception handling.

The Expected Result

Any unexpected error happened is handled and not causing chaos.

Solution

@ExceptionHandler(Exception.class)
public ResponseEntity<Object> handleGlobalException(Exception ex, WebRequest request) {
    // Extract the exception message
    String errorMessage = ex.getMessage();        
    
    // Return a simplified JSON response with the error message
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
        .body("{\"error\": \"" + errorMessage + "\"}");
}

Result

WhatsApp Image 2023-09-16 at 17 18 34_7ba2e7ea

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages