-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: arekkas <aeneas@ory.am>
- Loading branch information
arekkas
committed
Oct 9, 2018
1 parent
22bfa92
commit 7495be6
Showing
4 changed files
with
175 additions
and
1 deletion.
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
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
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,71 @@ | ||
/* | ||
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* | ||
* 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. | ||
* | ||
* @author Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @Copyright 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
package server | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ory/fosite" | ||
"github.com/ory/herodot" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type stackTracer interface { | ||
StackTrace() errors.StackTrace | ||
} | ||
|
||
type enhancedError struct { | ||
*fosite.RFC6749Error | ||
trace errors.StackTrace | ||
ID string `json:"request_id"` | ||
} | ||
|
||
func (e *enhancedError) StackTrace() errors.StackTrace { | ||
return e.trace | ||
} | ||
|
||
func ErrorEnhancerRFC6749(r *http.Request, err error) interface{} { | ||
var trace []errors.Frame | ||
|
||
if e, ok := err.(stackTracer); ok { | ||
trace = e.StackTrace() | ||
} | ||
|
||
if e, ok := errors.Cause(err).(*herodot.DefaultError); ok { | ||
|
||
err := &enhancedError{ | ||
RFC6749Error: &fosite.RFC6749Error{ | ||
Name: e.Error(), | ||
Description: e.Reason(), | ||
Code: e.StatusCode(), | ||
}, | ||
ID: r.Header.Get("X-Request-Id"), | ||
trace: trace, | ||
} | ||
return err | ||
} | ||
|
||
return &enhancedError{ | ||
RFC6749Error: fosite.ErrorToRFC6749Error(err), | ||
ID: r.Header.Get("X-Request-Id"), | ||
trace: trace, | ||
} | ||
} |
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,75 @@ | ||
/* | ||
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* | ||
* 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. | ||
* | ||
* @author Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @Copyright 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
package server | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
errors2 "errors" | ||
|
||
"github.com/ory/fosite" | ||
"github.com/ory/x/sqlcon" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestErrorEnhancer(t *testing.T) { | ||
for k, tc := range []struct { | ||
in error | ||
out string | ||
}{ | ||
{ | ||
in: sqlcon.ErrNoRows, | ||
out: "{\"error\":\"Unable to locate the resource\",\"error_description\":\"\",\"status_code\":404,\"request_id\":\"\"}", | ||
}, | ||
{ | ||
in: errors.WithStack(sqlcon.ErrNoRows), | ||
out: "{\"error\":\"Unable to locate the resource\",\"error_description\":\"\",\"status_code\":404,\"request_id\":\"\"}", | ||
}, | ||
{ | ||
in: errors.New("bla"), | ||
out: "{\"error\":\"error\",\"error_description\":\"The error is unrecognizable.\",\"status_code\":500,\"error_debug\":\"bla\",\"request_id\":\"\"}", | ||
}, | ||
{ | ||
in: errors2.New("bla"), | ||
out: "{\"error\":\"error\",\"error_description\":\"The error is unrecognizable.\",\"status_code\":500,\"error_debug\":\"bla\",\"request_id\":\"\"}", | ||
}, | ||
{ | ||
in: fosite.ErrInvalidRequest, | ||
out: "{\"error\":\"invalid_request\",\"error_description\":\"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed\",\"error_hint\":\"Make sure that the various parameters are correct, be aware of case sensitivity and trim your parameters. Make sure that the client you are using has exactly whitelisted the redirect_uri you specified.\",\"status_code\":400,\"request_id\":\"\"}", | ||
}, | ||
{ | ||
in: errors.WithStack(fosite.ErrInvalidRequest), | ||
out: "{\"error\":\"invalid_request\",\"error_description\":\"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed\",\"error_hint\":\"Make sure that the various parameters are correct, be aware of case sensitivity and trim your parameters. Make sure that the client you are using has exactly whitelisted the redirect_uri you specified.\",\"status_code\":400,\"request_id\":\"\"}", | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { | ||
err := ErrorEnhancerRFC6749(new(http.Request), tc.in) | ||
out, err2 := json.Marshal(err) | ||
require.NoError(t, err2) | ||
assert.Equal(t, tc.out, string(out)) | ||
}) | ||
} | ||
} |