Skip to content

Commit fa3ed69

Browse files
author
Anders Qvist
committed
Meeting simongottschlag comments on #252.
1 parent 368eee0 commit fa3ed69

File tree

7 files changed

+39
-27
lines changed

7 files changed

+39
-27
lines changed

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,30 @@ It is possible to add a custom function to handle errors. The error handler can
323323

324324
```go
325325
type Message struct {
326-
Message string `json:"message"`
327-
Url string `json:"url"`
326+
Message string `json:"message"`
327+
Url string `json:"url"`
328328
}
329329

330-
errorHandler := func(ctx context.Context, request *OidcError) *Response {
331-
message := Message {
332-
Message: request.status,
333-
Url: request.Url,
330+
func errorHandler(ctx context.Context, oidcErr *options.OidcError) *options.Response {
331+
message := Message{
332+
Message: string(oidcErr.Status),
333+
Url: oidcErr.Url.String(),
334334
}
335-
json, _ := json.Marshal(message)
336-
return &options.Response {
337-
Status: 418,
338-
Body: json,
335+
var headers map[string]string
336+
json, err := json.Marshal(message)
337+
if err != nil {
338+
headers["Content-Type"] = "text/plain"
339+
return &options.Response{
340+
StatusCode: 500,
341+
Headers: headers,
342+
Body: []byte("Internal encoding failure\r\n"),
343+
}
344+
}
345+
headers["Content-Type"] = "text/plain"
346+
return &options.Response{
347+
StatusCode: 418,
348+
Headers: headers,
349+
Body: json,
339350
}
340351
}
341352

@@ -347,6 +358,8 @@ oidcHandler := oidcgin.New(
347358
)
348359
```
349360

361+
This error handling interface was changed in v0.0.42. The previous interface was `func(description ErrorDescription, err error)`. In order to retain the same behavior, you need to update your error handler to read `desctiption` and `err` from `oidcErr` and return `nil`.
362+
350363
### Testing with the middleware enabled
351364

352365
There's a small package that simulates an OpenID Provider that can be used with tests.

internal/oidctesting/tests.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,14 @@ func runTestErrorHandler(t *testing.T, testName string, tester tester) {
321321
}{
322322
{
323323
testDescription: "no output",
324-
errorHandler: func(ctx context.Context, request *options.OidcError) *options.Response { return nil },
324+
errorHandler: func(ctx context.Context, oidcErr *options.OidcError) *options.Response { return nil },
325325
expectStatusCode: http.StatusBadRequest,
326326
expectHeaders: map[string]string{},
327327
expectBodyContains: []byte{},
328328
},
329329
{
330330
testDescription: "basic propagation",
331-
errorHandler: func(ctx context.Context, request *options.OidcError) *options.Response {
331+
errorHandler: func(ctx context.Context, oidcErr *options.OidcError) *options.Response {
332332
return &options.Response{
333333
StatusCode: 418,
334334
Headers: map[string]string{},
@@ -343,7 +343,7 @@ func runTestErrorHandler(t *testing.T, testName string, tester tester) {
343343
},
344344
{
345345
testDescription: "additional header",
346-
errorHandler: func(ctx context.Context, request *options.OidcError) *options.Response {
346+
errorHandler: func(ctx context.Context, oidcErr *options.OidcError) *options.Response {
347347
return &options.Response{
348348
StatusCode: 418,
349349
Headers: map[string]string{"some": "header"},
@@ -359,7 +359,7 @@ func runTestErrorHandler(t *testing.T, testName string, tester tester) {
359359
},
360360
{
361361
testDescription: "content type",
362-
errorHandler: func(ctx context.Context, request *options.OidcError) *options.Response {
362+
errorHandler: func(ctx context.Context, oidcErr *options.OidcError) *options.Response {
363363
return &options.Response{
364364
StatusCode: 418,
365365
Headers: map[string]string{"content-type": "application/json"},

oidcecho/echo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ func onError(c echo.Context, errorHandler options.ErrorHandler, statusCode int,
2424
c.Logger().Error(err)
2525
return c.NoContent(statusCode)
2626
}
27-
error := options.OidcError{
27+
oidcErr := options.OidcError{
2828
Url: c.Request().URL,
2929
Headers: c.Request().Header,
3030
Status: description,
3131
Error: err,
3232
}
33-
response := errorHandler(c.Request().Context(), &error)
33+
response := errorHandler(c.Request().Context(), &oidcErr)
3434
if response == nil {
3535
c.Logger().Error(err)
3636
return c.NoContent(statusCode)

oidcfiber/fiber.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ func onError(c *fiber.Ctx, errorHandler options.ErrorHandler, statusCode int, de
2929
for k, v := range c.GetReqHeaders() {
3030
headers[k] = []string{v}
3131
}
32-
error := options.OidcError{
32+
oidcErr := options.OidcError{
3333
Url: url,
3434
Headers: headers,
3535
Status: description,
3636
Error: err,
3737
}
38-
response := errorHandler(c.Context(), &error)
38+
response := errorHandler(c.Context(), &oidcErr)
3939
if response == nil {
4040
return c.SendStatus(statusCode)
4141
}

oidcgin/gin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func onError(c *gin.Context, errorHandler options.ErrorHandler, statusCode int,
2525
return c.AbortWithError(statusCode, err)
2626
}
2727

28-
error := options.OidcError{
28+
oidcErr := options.OidcError{
2929
Url: c.Request.URL,
3030
Headers: c.Request.Header,
3131
Status: description,
3232
Error: err,
3333
}
34-
response := errorHandler(c.Request.Context(), &error)
34+
response := errorHandler(c.Request.Context(), &oidcErr)
3535
if response == nil {
3636
return c.AbortWithError(statusCode, err)
3737
}

oidchttp/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func onError(r *http.Request, w http.ResponseWriter, errorHandler options.ErrorH
2525
w.WriteHeader(statusCode)
2626
return
2727
}
28-
error := options.OidcError{
28+
oidcErr := options.OidcError{
2929
Url: r.URL,
3030
Headers: r.Header,
3131
Status: description,
3232
Error: err,
3333
}
34-
response := errorHandler(r.Context(), &error)
34+
response := errorHandler(r.Context(), &oidcErr)
3535
if response == nil {
3636
w.WriteHeader(statusCode)
3737
return

options/options.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ import (
77
"time"
88
)
99

10-
// Context information for the error handler.
10+
// OidcError contains context information for the error handler.
1111
type OidcError struct {
1212
Url *url.URL
1313
Headers http.Header
1414
Error error
1515
Status ErrorDescription
1616
}
1717

18-
// Error handlers are expected to produce an abstract HTTP response that
19-
// the framework adapter will render.
18+
// Response holds an abstract HTTP response that the framework adapter will render.
2019
type Response struct {
2120
StatusCode int
2221
Headers map[string]string
2322
Body []byte
2423
}
2524

26-
// Return the content-type header from this response, or "applicatin/octet-stream"
25+
// ContentType returns the content-type header from this response, or "applicatin/octet-stream"
2726
// as per HTTP standard.
2827
func (r *Response) ContentType() string {
2928
for k, v := range r.Headers {
@@ -48,7 +47,7 @@ type ClaimsContextKeyName string
4847
const DefaultClaimsContextKeyName ClaimsContextKeyName = "claims"
4948

5049
// ErrorHandler is called by the middleware if not nil
51-
type ErrorHandler func(ctx context.Context, request *OidcError) *Response
50+
type ErrorHandler func(ctx context.Context, oidcErr *OidcError) *Response
5251

5352
// ErrorDescription is used to pass the description of the error to ErrorHandler
5453
type ErrorDescription string

0 commit comments

Comments
 (0)