-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -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; | ||
|
37 changes: 37 additions & 0 deletions
37
core/src/main/java/org/nzbhydra/web/errors/HydraErrorAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -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 { | ||
|
||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters