|
1 | 1 | package oidc
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "errors"
|
5 | 6 | "fmt"
|
6 | 7 | "log/slog"
|
@@ -133,6 +134,24 @@ type Error struct {
|
133 | 134 | Description string `json:"error_description,omitempty" schema:"error_description,omitempty"`
|
134 | 135 | State string `json:"state,omitempty" schema:"state,omitempty"`
|
135 | 136 | redirectDisabled bool `schema:"-"`
|
| 137 | + returnParent bool `schema:"-"` |
| 138 | +} |
| 139 | + |
| 140 | +func (e *Error) MarshalJSON() ([]byte, error) { |
| 141 | + m := struct { |
| 142 | + Error errorType `json:"error"` |
| 143 | + ErrorDescription string `json:"error_description,omitempty"` |
| 144 | + State string `json:"state,omitempty"` |
| 145 | + Parent string `json:"parent,omitempty"` |
| 146 | + }{ |
| 147 | + Error: e.ErrorType, |
| 148 | + ErrorDescription: e.Description, |
| 149 | + State: e.State, |
| 150 | + } |
| 151 | + if e.returnParent { |
| 152 | + m.Parent = e.Parent.Error() |
| 153 | + } |
| 154 | + return json.Marshal(m) |
136 | 155 | }
|
137 | 156 |
|
138 | 157 | func (e *Error) Error() string {
|
@@ -165,6 +184,18 @@ func (e *Error) WithParent(err error) *Error {
|
165 | 184 | return e
|
166 | 185 | }
|
167 | 186 |
|
| 187 | +// WithReturnParentToClient allows returning the set parent error to the HTTP client. |
| 188 | +// Currently it only supports setting the parent inside JSON responses, not redirect URLs. |
| 189 | +// As Go errors don't unmarshal well, only the marshaller is implemented for the moment. |
| 190 | +// |
| 191 | +// Warning: parent errors may contain sensitive data or unwanted details about the server status. |
| 192 | +// Also, the `parent` field is not a standard error field and might confuse certain clients |
| 193 | +// that require fully compliant responses. |
| 194 | +func (e *Error) WithReturnParentToClient(b bool) *Error { |
| 195 | + e.returnParent = b |
| 196 | + return e |
| 197 | +} |
| 198 | + |
168 | 199 | func (e *Error) WithDescription(desc string, args ...any) *Error {
|
169 | 200 | e.Description = fmt.Sprintf(desc, args...)
|
170 | 201 | return e
|
|
0 commit comments