graphql: errorcode int

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2023-10-25 10:39:23 +08:00
parent 8927c49bdd
commit 8cb7cff8d9

View file

@ -18,30 +18,34 @@
package graphql package graphql
import ( import (
"errors"
"fmt" "fmt"
) )
var ( var (
errBlockInvariant = errors.New("block objects must be instantiated with at least one of num or hash") errBlockInvariant = invalidParamsError("block objects must be instantiated with at least one of num or hash")
errInvalidBlockRange = errors.New("invalid from and to block combination: from > to") errInvalidBlockRange = invalidParamsError("invalid from and to block combination: from > to")
)
const (
errcodeDefault = -32600
errcodeInvalidParams = -32602
) )
type graphQLError struct { type graphQLError struct {
Code string `json:"code"` Code int
Message string `json:"message"` Message string
} }
// Error implements the github.com/graph-gophers/graphql-go.ResolverError interface. // Error implements the github.com/graph-gophers/graphql-go.ResolverError interface.
func (e *graphQLError) Error() string { func (e *graphQLError) Error() string {
return fmt.Sprintf("error [%s]: %s", e.Code, e.Message) return fmt.Sprintf("error [%d]: %s", e.Code, e.Message)
} }
// Extensions implements the github.com/graph-gophers/graphql-go.ResolverError interface. // Extensions implements the github.com/graph-gophers/graphql-go.ResolverError interface.
func (e *graphQLError) Extensions() map[string]interface{} { func (e *graphQLError) Extensions() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"code": e.Code, "errorCode": e.Code,
"message": e.Message, "errorMessage": e.Message,
} }
} }
@ -50,5 +54,9 @@ func asGraphQLError(err error) error {
if err == nil { if err == nil {
return nil return nil
} }
return &graphQLError{Code: "-32602", Message: err.Error()} return &graphQLError{Code: errcodeDefault, Message: err.Error()}
}
func invalidParamsError(msg string) error {
return &graphQLError{Code: errcodeInvalidParams, Message: msg}
} }