-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option for custom panic handler (#468)
Add option for custom panic handler
- Loading branch information
1 parent
5457f60
commit 446a2dd
Showing
6 changed files
with
61 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package errors | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// PanicHandler is the interface used to create custom panic errors that occur during query execution | ||
type PanicHandler interface { | ||
MakePanicError(ctx context.Context, value interface{}) *QueryError | ||
} | ||
|
||
// DefaultPanicHandler is the default PanicHandler | ||
type DefaultPanicHandler struct{} | ||
|
||
// MakePanicError creates a new QueryError from a panic that occurred during execution | ||
func (h *DefaultPanicHandler) MakePanicError(ctx context.Context, value interface{}) *QueryError { | ||
return Errorf("panic occurred: %v", value) | ||
} |
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,24 @@ | ||
package errors | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestDefaultPanicHandler(t *testing.T) { | ||
handler := &DefaultPanicHandler{} | ||
qErr := handler.MakePanicError(context.Background(), "foo") | ||
if qErr == nil { | ||
t.Fatal("Panic error must not be nil") | ||
} | ||
const ( | ||
expectedMessage = "panic occurred: foo" | ||
expectedError = "graphql: " + expectedMessage | ||
) | ||
if qErr.Error() != expectedError { | ||
t.Errorf("Unexpected panic error message: %q != %q", qErr.Error(), expectedError) | ||
} | ||
if qErr.Message != expectedMessage { | ||
t.Errorf("Unexpected panic QueryError.Message: %q != %q", qErr.Message, expectedMessage) | ||
} | ||
} |
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