All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.server.rest.RestExceptionHandler Maven / Gradle / Ivy

The newest version!
package com.xlrit.gears.server.rest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.xlrit.gears.base.exception.AuthException;
import com.xlrit.gears.base.exception.NotFoundException;

@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

	@ExceptionHandler(NotFoundException.class)
	@ResponseStatus(HttpStatus.NOT_FOUND)
	public ResponseEntity handleNotFoundException(NotFoundException exception, WebRequest request) {
		return ResponseEntity
			.status(HttpStatus.NOT_FOUND)
			.body(exception.getMessage());
	}

	@ExceptionHandler(AuthException.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public ResponseEntity handleAuthException(AuthException exception, WebRequest request) {
		return ResponseEntity
			.status(HttpStatus.UNAUTHORIZED)
			.body(exception.getMessage());
	}
}