title: Spring Security Oauth - Custom format for OAuth2Exceptions
body: <p>The error format of spring security oauth conforms with the OAuth spec and looks like this.</p>
  
  <pre><code>{
    "error":"insufficient_scope",
    "error_description":"Insufficient scope for this resource",
    "scope":"do.something"
  }
  </code></pre>
  
  <p>Especially on a resource server I find it a bit strange to get a different error format for authentication issues. So I would like to change the way this exception is rendered.</p>
  
  <p>The <a href="http://projects.spring.io/spring-security-oauth/docs/oauth2.html" rel="noreferrer">documentation</a> says</p>
  
  <blockquote>
    <p>Error handling in an Authorization Server uses standard Spring MVC
    features, namely @ExceptionHandler methods</p>
  </blockquote>
  
  <p>So I tried something like this to customize the format of the error:</p>
  
  <pre><code>@ControllerAdvice
  @Order(Ordered.HIGHEST_PRECEDENCE)
  public class MyErrorHandler {
  
      @ExceptionHandler(value = {InsufficientScopeException.class})
      ResponseEntity&lt;MyErrorRepresentation&gt; handle(RuntimeException ex, HttpServletRequest request) {
          return errorResponse(HttpStatus.FORBIDDEN,
                  MyErrorRepresentation.builder()
                          .errorId("insufficient.scope")
                          .build(),
                  request);
      }
  }
  </code></pre>
  
  <p>But this does not work. </p>
  
  <p>Looking at the code, all the error rendering seems to be done in <code>DefaultWebResponseExceptionTranslator#handleOAuth2Exception</code>. But implementing a custom <code>WebResponseExceptionTranslator</code> would not allow changing the format.</p>
  
  <p>Any hints?</p>