Open Kilda Java Documentation
NorthboundExceptionHandler.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.utils;
17 
18 import static java.lang.String.format;
21 
25 
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.web.bind.annotation.ControllerAdvice;
30 import org.springframework.web.bind.annotation.ExceptionHandler;
31 import org.springframework.web.context.request.WebRequest;
32 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
33 
34 import java.util.Optional;
35 
39 @ControllerAdvice
40 public class NorthboundExceptionHandler extends ResponseEntityExceptionHandler {
48  @ExceptionHandler(MessageException.class)
49  protected ResponseEntity<Object> handleMessageException(MessageException exception, WebRequest request) {
50  HttpStatus status;
51 
52  switch (exception.getErrorType()) {
53  case NOT_FOUND:
54  status = HttpStatus.NOT_FOUND;
55  break;
56  case DATA_INVALID:
57  status = HttpStatus.BAD_REQUEST;
58  break;
59  case PARAMETERS_INVALID:
60  status = HttpStatus.BAD_REQUEST;
61  break;
62  case ALREADY_EXISTS:
63  status = HttpStatus.CONFLICT;
64  break;
65  case AUTH_FAILED:
66  status = HttpStatus.UNAUTHORIZED;
67  break;
68  case OPERATION_TIMED_OUT:
69  case INTERNAL_ERROR:
70  status = HttpStatus.INTERNAL_SERVER_ERROR;
71  break;
72  default:
73  status = HttpStatus.INTERNAL_SERVER_ERROR;
74  break;
75  }
76 
77  MessageError error = new MessageError(exception.getCorrelationId(), exception.getTimestamp(),
78  exception.getErrorType().toString(), exception.getMessage(), exception.getErrorDescription());
79 
80  logger.warn(format("Error %s caught.", error), exception);
81 
82  return super.handleExceptionInternal(exception, error, new HttpHeaders(), status, request);
83  }
84 
88  @Override
89  protected ResponseEntity<Object> handleExceptionInternal(Exception exception, Object body, HttpHeaders headers,
90  HttpStatus status, WebRequest request) {
91  String correlationId = Optional.ofNullable(request.getHeader(CORRELATION_ID)).orElse(DEFAULT_CORRELATION_ID);
92  MessageError error = new MessageError(correlationId, System.currentTimeMillis(),
93  ErrorType.REQUEST_INVALID.toString(), exception.getMessage(), exception.getClass().getSimpleName());
94 
95  logger.error(format("Unknown error %s caught.", error), exception);
96 
97  return super.handleExceptionInternal(exception, error, headers, status, request);
98  }
99 }
ResponseEntity< Object > handleExceptionInternal(Exception exception, Object body, HttpHeaders headers, HttpStatus status, WebRequest request)
def status()
Definition: rest.py:593
static final String DEFAULT_CORRELATION_ID
Definition: Utils.java:69
static final String CORRELATION_ID
Definition: Utils.java:43
ResponseEntity< Object > handleMessageException(MessageException exception, WebRequest request)