{"action":"created","issue":{"url":"https://api.github.com/repos/spring-projects/spring-security-oauth/issues/387","labels_url":"https://api.github.com/repos/spring-projects/spring-security-oauth/issues/387/labels{/name}","comments_url":"https://api.github.com/repos/spring-projects/spring-security-oauth/issues/387/comments","events_url":"https://api.github.com/repos/spring-projects/spring-security-oauth/issues/387/events","html_url":"https://github.com/spring-projects/spring-security-oauth/issues/387","id":56602174,"number":387,"title":"OAuth2RestTemplate never delegates to custom error handlers","user":{"login":"mressler","id":605015,"avatar_url":"https://avatars.githubusercontent.com/u/605015?v=3","gravatar_id":"","url":"https://api.github.com/users/mressler","html_url":"https://github.com/mressler","followers_url":"https://api.github.com/users/mressler/followers","following_url":"https://api.github.com/users/mressler/following{/other_user}","gists_url":"https://api.github.com/users/mressler/gists{/gist_id}","starred_url":"https://api.github.com/users/mressler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mressler/subscriptions","organizations_url":"https://api.github.com/users/mressler/orgs","repos_url":"https://api.github.com/users/mressler/repos","events_url":"https://api.github.com/users/mressler/events{/privacy}","received_events_url":"https://api.github.com/users/mressler/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/spring-projects/spring-security-oauth/labels/Improvement","name":"Improvement","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/spring-projects/spring-security-oauth/milestones/12","labels_url":"https://api.github.com/repos/spring-projects/spring-security-oauth/milestones/12/labels","id":956705,"number":12,"title":"2.0.7","description":"","creator":{"login":"dsyer","id":124075,"avatar_url":"https://avatars.githubusercontent.com/u/124075?v=3","gravatar_id":"","url":"https://api.github.com/users/dsyer","html_url":"https://github.com/dsyer","followers_url":"https://api.github.com/users/dsyer/followers","following_url":"https://api.github.com/users/dsyer/following{/other_user}","gists_url":"https://api.github.com/users/dsyer/gists{/gist_id}","starred_url":"https://api.github.com/users/dsyer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsyer/subscriptions","organizations_url":"https://api.github.com/users/dsyer/orgs","repos_url":"https://api.github.com/users/dsyer/repos","events_url":"https://api.github.com/users/dsyer/events{/privacy}","received_events_url":"https://api.github.com/users/dsyer/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":0,"state":"open","created_at":"2015-01-29T12:51:01Z","updated_at":"2015-02-06T09:20:55Z","due_on":null,"closed_at":null},"comments":4,"created_at":"2015-02-04T23:08:42Z","updated_at":"2015-02-06T15:48:28Z","closed_at":null,"body":"While testing my Spring OAuth2 implementation (thank you!) I noticed that if my server returned a 403 Forbidden, my `OAuth2RestTemplate` would only ever throw `OAuth2Exceptions` instead of the expected `HttpClientErrorException`.  Further, the custom error handler that I had set up was never being called.\r\n\r\nIt looks like the `setErrorHandler` call in the `OAuth2RestTemplate` wraps my error custom error handler in an `OAuth2ErrorHandler`.  By itself, this is fine and I'm glad that there are standard errors that can be sniffed out and handled in this way.  My problem is on line 127 of `OAuth2ErrorHandler`:https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/main/java/org/springframework/security/oauth2/client/http/OAuth2ErrorHandler.java#L127\r\n\r\n```\r\nOAuth2Exception body = extractor.extractData(bufferedResponse);\r\nif (body != null) {\r\n```\r\n\r\nLooking at the `OAuth2ExceptionJackson2Deserializer`, it looks like line 127 will always return a non-null body object.  In my particular case, I'm returning my own custom JSON error content for the 403 result, but I'm still getting the `OAuth2Exception` deserialized from that line 127.  And, since the body object is non-null, the error handler never delegates to my custom error handler (line 148).\r\n\r\nIt seems like line 128 should be doing something a little more specific than just making sure it's non-null.  In my case, the resulting JSON was not intended to be turned into an `OAuth2Exception`.  Shouldn't line 128 looks for a return value that is a subclass of `OAuth2Exception`?\r\n"},"comment":{"url":"https://api.github.com/repos/spring-projects/spring-security-oauth/issues/comments/73257100","html_url":"https://github.com/spring-projects/spring-security-oauth/issues/387#issuecomment-73257100","issue_url":"https://api.github.com/repos/spring-projects/spring-security-oauth/issues/387","id":73257100,"user":{"login":"mressler","id":605015,"avatar_url":"https://avatars.githubusercontent.com/u/605015?v=3","gravatar_id":"","url":"https://api.github.com/users/mressler","html_url":"https://github.com/mressler","followers_url":"https://api.github.com/users/mressler/followers","following_url":"https://api.github.com/users/mressler/following{/other_user}","gists_url":"https://api.github.com/users/mressler/gists{/gist_id}","starred_url":"https://api.github.com/users/mressler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mressler/subscriptions","organizations_url":"https://api.github.com/users/mressler/orgs","repos_url":"https://api.github.com/users/mressler/repos","events_url":"https://api.github.com/users/mressler/events{/privacy}","received_events_url":"https://api.github.com/users/mressler/received_events","type":"User","site_admin":false},"created_at":"2015-02-06T15:48:28Z","updated_at":"2015-02-06T15:48:28Z","body":"For the time being I'm just checking that I get an `OAuth2Exception` and ignore the status code.  Perhaps if we didn't hardcode the status code in the `OAuth2Exception`?  Allowing for the status code to be modified in the `OAuth2ErrorHandler` would mean I could verify that at least the status code is correct.\r\n\r\nI could send a pull request that would add that functionality if you think it's a good workaround.\r\n\r\nThat doesn't fix the custom error handler though.\r\n\r\nFor that, I'd suggest that if the `bufferedResponse.getRawStatusCode()` doesn't match the caught exception's `getStatusCode()` then the machinery has picked up an error that it shouldn't have and the `errorHandler` should get called instead of rethrowing the exception."}}