Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. Joni Karppinen revised this gist Dec 12, 2015. No changes.
  2. Joni Karppinen revised this gist Mar 19, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions CustomErrorController.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    package com.company.project.controllers;

    import com.company.project.model.api.Error;
    import com.company.project.model.api.ErrorJson;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.web.ErrorAttributes;
    @@ -33,10 +33,10 @@ public class CustomErrorController implements ErrorController {
    private ErrorAttributes errorAttributes;

    @RequestMapping(value = PATH)
    Error error(HttpServletRequest request, HttpServletResponse response) {
    ErrorJson error(HttpServletRequest request, HttpServletResponse response) {
    // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring.
    // Here we just define response body.
    return new Error(response.getStatus(), getErrorAttributes(request, debug));
    return new ErrorJson(response.getStatus(), getErrorAttributes(request, debug));
    }

    @Override
  3. Joni Karppinen revised this gist Feb 20, 2015. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions CustomErrorController.java
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,9 @@
    import java.util.Map;

    /**
    * Based on the helpful answer at http://stackoverflow.com/q/25356781/56285,
    * with error details in response body added.
    *
    * @author Joni Karppinen
    * @since 20.2.2015
    */
  4. Joni Karppinen renamed this gist Feb 20, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion CustomErrorController → CustomErrorController.java
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,8 @@ public class CustomErrorController implements ErrorController {

    @RequestMapping(value = PATH)
    Error error(HttpServletRequest request, HttpServletResponse response) {
    // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. Here we just define response body.
    // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring.
    // Here we just define response body.
    return new Error(response.getStatus(), getErrorAttributes(request, debug));
    }

  5. Joni Karppinen created this gist Feb 20, 2015.
    48 changes: 48 additions & 0 deletions CustomErrorController
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    package com.company.project.controllers;

    import com.company.project.model.api.Error;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.web.ErrorAttributes;
    import org.springframework.boot.autoconfigure.web.ErrorController;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.context.request.RequestAttributes;
    import org.springframework.web.context.request.ServletRequestAttributes;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Map;

    /**
    * @author Joni Karppinen
    * @since 20.2.2015
    */
    @RestController
    public class CustomErrorController implements ErrorController {

    private static final String PATH = "/error";

    @Value("${debug}")
    private boolean debug;

    @Autowired
    private ErrorAttributes errorAttributes;

    @RequestMapping(value = PATH)
    Error error(HttpServletRequest request, HttpServletResponse response) {
    // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. Here we just define response body.
    return new Error(response.getStatus(), getErrorAttributes(request, debug));
    }

    @Override
    public String getErrorPath() {
    return PATH;
    }

    private Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    return errorAttributes.getErrorAttributes(requestAttributes, includeStackTrace);
    }

    }