Skip to content

Commit

Permalink
Use error attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherp committed Dec 1, 2024
1 parent 3c40107 commit 7ed4caf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright 2017 TheOtherP ([email protected])
* (C) Copyright 2024 TheOtherP ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.nzbhydra.web;
package org.nzbhydra.web.errors;

import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* (C) Copyright 2024 TheOtherP ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.nzbhydra.web.errors;

import org.nzbhydra.misc.StackTraceFilter;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Component
public class HydraErrorAttributes extends DefaultErrorAttributes {

@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Throwable throwable = getError(webRequest);
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
errorAttributes.put("trace", StackTraceFilter.getFilteredStackTrace(throwable));
return errorAttributes;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright 2023 TheOtherP ([email protected])
* (C) Copyright 2024 TheOtherP ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,25 +14,29 @@
* limitations under the License.
*/

package org.nzbhydra.web;
package org.nzbhydra.web.errors;

import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.nzbhydra.Jackson;
import org.nzbhydra.misc.StackTraceFilter;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

@Controller
//@Controller
@Slf4j
public class HydraErrorController extends AbstractErrorController implements ErrorController {

Expand All @@ -41,23 +45,20 @@ public HydraErrorController(ErrorAttributes errorAttributes) {
}

@RequestMapping("/error")
public ModelAndView handleError(HttpServletRequest request, HttpServletResponse response, Object handler) {
ModelAndView errorPage = new ModelAndView("error");
public ResponseEntity<Object> handleError(HttpServletRequest request, HttpServletResponse response, Object handler) {
WebRequest webRequest = new ServletWebRequest(request);
Throwable ex = new DefaultErrorAttributes().getError(webRequest);
if (ex != null) {
errorPage.addObject("exception", StackTraceFilter.getFilteredStackTrace(ex));
errorPage.addObject("error", ex.getMessage());
//Log for good measure, perhaps it wasn't already logged
log.debug("Handling exception", ex);
} else {
log.error("Cannot show filtered exception because it's null");
errorPage.addObject("exception", "<Unknown>");
errorPage.addObject("error", "<Unknown>");
Map<String, Object> map = new HashMap<>();
HttpStatus status = getStatus(request);
map.put("exception", StackTraceFilter.getFilteredStackTrace(ex));
map.put("error", ex.getMessage());
map.put("timestap", Instant.now());
try {
map.put("parameters", Jackson.JSON_MAPPER.writeValueAsString(request.getParameterMap()));
} catch (JsonProcessingException e) {
log.error("Error serializing parameters", e);
}

errorPage.addObject("status", response.getStatus());
errorPage.addObject("timestamp", Instant.now().toString());
return errorPage;
return ResponseEntity.status(status).body(map);
}
}
2 changes: 2 additions & 0 deletions core/src/main/resources/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ server.servlet.encoding.force=true
spring.thymeleaf.mode=HTML
server.shutdown=graceful
server.compression.enabled=true
server.error.include-exception=true
server.error.include-stacktrace=always

#Performance logging
#logging.level.org.thymeleaf=TRACE
Expand Down

0 comments on commit 7ed4caf

Please sign in to comment.