2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.main.web;
23 import io.swagger.v3.oas.annotations.Hidden;
25 import javax.servlet.RequestDispatcher;
26 import javax.servlet.http.HttpServletRequest;
27 import org.onap.policy.clamp.controlloop.models.messages.rest.SimpleResponse;
28 import org.onap.policy.clamp.controlloop.models.messages.rest.TypedSimpleResponse;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.boot.web.error.ErrorAttributeOptions;
33 import org.springframework.boot.web.servlet.error.ErrorAttributes;
34 import org.springframework.boot.web.servlet.error.ErrorController;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.MediaType;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.context.request.ServletWebRequest;
44 public class RuntimeErrorController implements ErrorController {
46 private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeErrorController.class);
48 private final ErrorAttributes errorAttributes;
50 @Value("${server.error.path}")
56 * @param errorAttributes ErrorAttributes
58 public RuntimeErrorController(ErrorAttributes errorAttributes) {
59 this.errorAttributes = errorAttributes;
62 protected HttpStatus getStatus(HttpServletRequest request) {
63 Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
64 if (statusCode == null) {
65 return HttpStatus.INTERNAL_SERVER_ERROR;
68 return HttpStatus.valueOf(statusCode);
69 } catch (Exception ex) {
70 LOGGER.error("statusCode {} Not Valid", statusCode, ex);
71 return HttpStatus.INTERNAL_SERVER_ERROR;
76 * Handle Errors not handled to GlobalControllerExceptionHandler.
78 * @param request HttpServletRequest
79 * @return ResponseEntity
81 @RequestMapping(value = "${server.error.path}", produces = MediaType.APPLICATION_JSON_VALUE)
82 public ResponseEntity<TypedSimpleResponse<SimpleResponse>> handleError(HttpServletRequest request) {
83 Map<String, Object> map = this.errorAttributes.getErrorAttributes(new ServletWebRequest(request),
84 ErrorAttributeOptions.defaults());
86 StringBuilder sb = new StringBuilder();
87 final Object error = map.get("error");
89 sb.append(error.toString()).append(" ");
91 final Object message = map.get("message");
92 if (message != null) {
93 sb.append(message.toString());
96 TypedSimpleResponse<SimpleResponse> resp = new TypedSimpleResponse<>();
97 resp.setErrorDetails(sb.toString());
99 return ResponseEntity.status(getStatus(request)).body(resp);