-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom schema errors #18
Merged
Merged
Conversation
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
template: main.go.tmpl:8:39: executing "main" at <eq .Opts.legacyErrors "true">: error calling eq: incompatible types for comparison
See https://github.com/webrpc/webrpc/tree/master/tests. No need to maintain this twice.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduction
1. Define your own custom schema errors in RIDL file, for example:
Note: Unless specified, the default HTTP status for webrpc errors is
HTTP 400
.2. Return your custom schema error from your RPC endpoint:
You can then assert the error from the client:
3. Return your custom schema error along with the underlying cause:
You can still assert the error type
errors.Is(err, proto.ErrRateLimited)
or get the underlying cause witherrors.Unwrap(err)
.3. Return a generic Go error from your RPC endpoint (HTTP 400):
Breaking API changes
1. Deprecates existing werbrpc error functions and sentinel errors:
proto.WrapError() // Deprecated.
proto.Errorf() // Deprecated.
proto.HTTPStatusFromErrorCode()
proto.IsErrorCode()
proto.ErrCanceled // Deprecated.
proto.ErrUnknown // Deprecated.
proto.ErrFail // Deprecated.
proto.ErrInvalidArgument // Deprecated.
proto.ErrDeadlineExceeded // Deprecated.
proto.ErrNotFound // Deprecated.
proto.ErrBadRoute // Deprecated.
proto.ErrAlreadyExists // Deprecated.
proto.ErrPermissionDenied // Deprecated.
proto.ErrUnauthenticated // Deprecated.
proto.ErrResourceExhausted // Deprecated.
proto.ErrFailedPrecondition // Deprecated.
proto.ErrAborted // Deprecated.
proto.ErrOutOfRange // Deprecated.
proto.ErrUnimplemented // Deprecated.
proto.ErrInternal // Deprecated.
proto.ErrUnavailable // Deprecated.
proto.ErrDataLoss // Deprecated.
proto.ErrNone // Deprecated.
Note: You can bring these back via
-legacyErrors=true
webrpc-gen flag. This will allow you to gradually migrate to schema errors, while keeping your existing codebase working.Breaking changes in the generated error code:
WebRPCError
implements Go'serror
interface, carries HTTP status code and has a first-class support for (un)wrapping introduced in Go 1.13:errors.Is(err, proto.RateLimited)
cause := errors.Unwrap(err)
Migrate gradually with
-legacyErrors=true
flagIf you enable
-legacyErrors=true
flag on the webrpc-gen golang template, it will generate backward-compatible code for the deprecatedErrorf()
andWrapError()
functions and predefinedErr*
error codes. All this code will be marked asdeprecated, which should help you migrate to theErrorWithCause()
function once you start creating custom errors in your RIDL schema.