1 package org.onap.ccsdk.apps.services;
3 import org.jvnet.hk2.annotations.Service;
4 import org.springframework.beans.ConversionNotSupportedException;
5 import org.springframework.beans.TypeMismatchException;
6 import org.springframework.core.Ordered;
7 import org.springframework.core.annotation.Order;
8 import org.springframework.http.HttpHeaders;
9 import org.springframework.http.HttpStatus;
10 import org.springframework.http.HttpStatusCode;
11 import org.springframework.http.ResponseEntity;
12 import org.springframework.http.converter.HttpMessageNotReadableException;
13 import org.springframework.http.converter.HttpMessageNotWritableException;
14 import org.springframework.validation.BindException;
15 import org.springframework.web.HttpMediaTypeNotAcceptableException;
16 import org.springframework.web.HttpMediaTypeNotSupportedException;
17 import org.springframework.web.HttpRequestMethodNotSupportedException;
18 import org.springframework.web.bind.MethodArgumentNotValidException;
19 import org.springframework.web.bind.MissingPathVariableException;
20 import org.springframework.web.bind.MissingServletRequestParameterException;
21 import org.springframework.web.bind.ServletRequestBindingException;
22 import org.springframework.web.bind.annotation.ControllerAdvice;
23 import org.springframework.web.bind.annotation.ExceptionHandler;
24 import org.springframework.web.bind.annotation.RestController;
25 import org.springframework.web.bind.annotation.RestControllerAdvice;
26 import org.springframework.web.context.request.WebRequest;
27 import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
28 import org.springframework.web.multipart.support.MissingServletRequestPartException;
29 import org.springframework.web.servlet.NoHandlerFoundException;
30 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
31 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
33 import jakarta.servlet.http.HttpServletRequest;
35 @Order(Ordered.HIGHEST_PRECEDENCE)
37 public class RestExceptionHandler extends ResponseEntityExceptionHandler {
41 protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
42 return createResponseEntity(new RestProtocolError("bad-method", "Method not supported", ex), status);
46 protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
47 return createResponseEntity(new RestProtocolError("bad-media-type", "Media type not supported", ex), status);
51 protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
52 return createResponseEntity(new RestProtocolError("bad-media-type", "Media type not acceptable", ex), status);
56 protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
57 return createResponseEntity(new RestApplicationError("missing-path", "Missing path variable", ex), status);
61 protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
62 return createResponseEntity(new RestProtocolError("missing-param", "Missing servlet request parameter", ex), status);
66 protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
67 return createResponseEntity(new RestApplicationError("request-binding", "Servlet binding exception", ex), status);
71 protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
72 return createResponseEntity(new RestApplicationError("conversion", "Conversion not supported", ex), status);
76 protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
77 return createResponseEntity(new RestProtocolError("type-mismatch", "Type mismatch", ex), status);
81 protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
82 return createResponseEntity(new RestProtocolError("bad-message", "HTTP message not readable", ex), status);
86 protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
87 return createResponseEntity(new RestProtocolError("bad-message", "HTTP message not writable", ex), status);
91 protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
92 return createResponseEntity(new RestProtocolError("bad-message", "Method argument not valid", ex), status);
96 protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
97 return createResponseEntity(new RestProtocolError("bad-message", "Missing servlet request part", ex), status);
101 protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
102 return createResponseEntity(new RestApplicationError("binding-error", "Missing servlet request part", ex), status);
106 protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
107 return createResponseEntity(new RestApplicationError("binding-error", "No handler found", ex), status);
111 protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest webRequest) {
112 return createResponseEntity(new RestApplicationError("timeout", "Async request timeout", ex), status);
116 protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
117 return createResponseEntity(new RestApplicationError("internal-error", "Internal error", ex), status);
120 @ExceptionHandler({RestException.class})
121 protected ResponseEntity<Object> handleRestException(RestException ex, HttpServletRequest request) {
122 RestError error = ex.getRestError();
123 if (request != null) {
124 error.setErrorPath(request.getServletPath());
126 return createResponseEntity(error,HttpStatusCode.valueOf(ex.getStatus()));
129 private ResponseEntity<Object> createResponseEntity(RestError restError, HttpStatusCode status) {
131 return new ResponseEntity<>(new RestErrors(restError), status);