Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #978 from 99designs/pluggable-error-code
Allow customizing http and websocket status codes for errors
- Loading branch information
Showing
30 changed files
with
5,758 additions
and
4,941 deletions.
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
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
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
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,49 @@ | ||
package errcode | ||
|
||
import ( | ||
"github.com/vektah/gqlparser/gqlerror" | ||
) | ||
|
||
const ValidationFailed = "GRAPHQL_VALIDATION_FAILED" | ||
const ParseFailed = "GRAPHQL_PARSE_FAILED" | ||
|
||
type ErrorKind int | ||
|
||
const ( | ||
// issues with graphql (validation, parsing). 422s in http, GQL_ERROR in websocket | ||
KindProtocol ErrorKind = iota | ||
// user errors, 200s in http, GQL_DATA in websocket | ||
KindUser | ||
) | ||
|
||
var codeType = map[string]ErrorKind{ | ||
ValidationFailed: KindProtocol, | ||
ParseFailed: KindProtocol, | ||
} | ||
|
||
// RegisterErrorType should be called by extensions that want to customize the http status codes for errors they return | ||
func RegisterErrorType(code string, kind ErrorKind) { | ||
codeType[code] = kind | ||
} | ||
|
||
// Set the error code on a given graphql error extension | ||
func Set(err *gqlerror.Error, value string) { | ||
if err.Extensions == nil { | ||
err.Extensions = map[string]interface{}{} | ||
} | ||
|
||
err.Extensions["code"] = value | ||
} | ||
|
||
// get the kind of the first non User error, defaults to User if no errors have a custom extension | ||
func GetErrorKind(errs gqlerror.List) ErrorKind { | ||
for _, err := range errs { | ||
if code, ok := err.Extensions["code"].(string); ok { | ||
if kind, ok := codeType[code]; ok && kind != KindUser { | ||
return kind | ||
} | ||
} | ||
} | ||
|
||
return KindUser | ||
} |
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
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
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
Oops, something went wrong.