Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Exceptionhandler @ControllerAdvice not working as it should #25070

Closed
rshtishi opened this issue May 13, 2020 · 1 comment
Closed

Custom Exceptionhandler @ControllerAdvice not working as it should #25070

rshtishi opened this issue May 13, 2020 · 1 comment
Labels
status: invalid An issue that we don't feel is valid

Comments

@rshtishi
Copy link

rshtishi commented May 13, 2020

Hi,

I created a RestExceptionHandler to manage all exceptions thrown from RestControllers. I am having a problem that RestExceptionHandler is not intercepting the exception and returning the custom message format.

The RestExceptionhandler implementation:

@Order(Ordered.HIGHEST_PRECEDENCE) 
@ControllerAdvice(annotations = RestController.class)
public class RestExceptionHandler
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);

	@ExceptionHandler(ResourceNotFoundException.class)
	public ResponseEntity<ErrorDetail> handleResourceNotFoundException(ResourceNotFoundException exception,
			HttpServletRequest request) {
		ErrorDetail errorDetail = new ErrorDetail();
		errorDetail.setTimeStamp(Instant.now().getEpochSecond());
		errorDetail.setStatus(HttpStatus.NOT_FOUND.value());
		errorDetail.setTitle("Resource Not Found");
		errorDetail.setDetail(exception.getMessage());
		errorDetail.setDeveloperMessage(exception.getClass().getName());
		return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
	}
}

The RestController implementation as below:

@RestController
@RequestMapping("/departments")
public class DepartmentRestController {

	@Autowired
	private DepartmentService departmentService;

	protected Department verifyDeparmentExistence(int id) {
		Department department = departmentService.findById(id);
		if (department != null) {
			throw new ResourceNotFoundException("Department with id: " + id + " not found.");
		}
		return department;
	}

	@GetMapping
	public ResponseEntity<List<Department>> findAll() {
		List<Department> departmentList = departmentService.findAll();
		return new ResponseEntity<>(departmentList, HttpStatus.OK);
	}

	@GetMapping("/{id}")
	public ResponseEntity<Department> findById(@PathVariable int id) {
		Department department = verifyDeparmentExistence(id);
		return new ResponseEntity<>(department, HttpStatus.OK);
	}
}

The ResourceNotFoundException is like below:

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
	
	private static final long serialVersionUID = 1L;
	
	public ResourceNotFoundException() {}
	
	public ResourceNotFoundException(String message) {
		super(message);
	}
	
	public ResourceNotFoundException(String message, Throwable cause) {
		super(message,cause);
	}

}

The ErrorDetail (the custom message format that should be returned) :

@Data
public class ErrorDetail {
	
	private String title;
	private int status;
	private String detail;
	private long timeStamp;
	private String path;
	private String developerMessage;
	private Map<String, List<ValidationError>> errors = new HashMap<>();
}

The full project you can find it the link below :
https://github.com/rshtishi/payroll/tree/master/department

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 13, 2020
@rshtishi
Copy link
Author

rshtishi commented May 14, 2020

Hello,

I fixed the issue. Another exception was thrown before ResourceNotFoundException. That was caught by GlobalExceptionHanlder since I didn't specify a handler for that type of exception in my custom RestExceptionhandler. I fixed that and everything is working as it should. You can close the issue.

@sbrannen sbrannen added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants