diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 7263136..85ef29b 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -151,7 +151,7 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) defer func() { // In case of a panic, serve a HTTP 500 error and then panic. if rr := recover(); rr != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcServerPanic, fmt.Errorf("%v", rr))) + RespondWithError(w, ErrWebrpcServerPanic.WithCause(fmt.Errorf("%v", rr))) panic(rr) } }() @@ -176,14 +176,14 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) case "/rpc/ExampleService/LogEvent": handler = s.serveLogEventJSON default: - err := ErrorWithCause(ErrWebrpcBadRoute, fmt.Errorf("no handler for path %q", r.URL.Path)) + err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no handler for path %q", r.URL.Path)) RespondWithError(w, err) return } if r.Method != "POST" { w.Header().Add("Allow", "POST") // RFC 9110. - err := ErrorWithCause(ErrWebrpcBadMethod, fmt.Errorf("unsupported method %q (only POST is allowed)", r.Method)) + err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %q (only POST is allowed)", r.Method)) RespondWithError(w, err) return } @@ -198,7 +198,7 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) case "application/json": handler(ctx, w, r) default: - err := ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("unexpected Content-Type: %q", r.Header.Get("Content-Type"))) + err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unexpected Content-Type: %q", r.Header.Get("Content-Type"))) RespondWithError(w, err) } } @@ -233,7 +233,7 @@ func (s *exampleServiceServer) serveStatusJSON(ctx context.Context, w http.Respo }{ret0} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to marshal json response: %w", err))) + RespondWithError(w, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) return } @@ -257,7 +257,7 @@ func (s *exampleServiceServer) serveVersionJSON(ctx context.Context, w http.Resp }{ret0} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to marshal json response: %w", err))) + RespondWithError(w, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) return } @@ -271,7 +271,7 @@ func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.Resp reqBody, err := io.ReadAll(r.Body) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) return } defer r.Body.Close() @@ -281,7 +281,7 @@ func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.Resp Arg1 uint64 `json:"userID"` }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to unmarshal request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) return } @@ -297,7 +297,7 @@ func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.Resp }{ret0} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to marshal json response: %w", err))) + RespondWithError(w, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) return } @@ -311,7 +311,7 @@ func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.Res reqBody, err := io.ReadAll(r.Body) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) return } defer r.Body.Close() @@ -320,7 +320,7 @@ func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.Res Arg0 *SearchFilter `json:"s"` }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to unmarshal request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) return } @@ -337,7 +337,7 @@ func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.Res }{ret0, ret1} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to marshal json response: %w", err))) + RespondWithError(w, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) return } @@ -351,7 +351,7 @@ func (s *exampleServiceServer) serveLogEventJSON(ctx context.Context, w http.Res reqBody, err := io.ReadAll(r.Body) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) return } defer r.Body.Close() @@ -360,7 +360,7 @@ func (s *exampleServiceServer) serveLogEventJSON(ctx context.Context, w http.Res Arg0 string `json:"event"` }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to unmarshal request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) return } @@ -379,7 +379,7 @@ func (s *exampleServiceServer) serveLogEventJSON(ctx context.Context, w http.Res func RespondWithError(w http.ResponseWriter, err error) { rpcErr, ok := err.(WebRPCError) if !ok { - rpcErr = ErrorWithCause(ErrWebrpcEndpoint, err) + rpcErr = ErrWebrpcEndpoint.WithCause(err) } w.Header().Set("Content-Type", "application/json") @@ -517,41 +517,41 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType func doJSONRequest(ctx context.Context, client HTTPClient, url string, in, out interface{}) error { reqBody, err := json.Marshal(in) if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("failed to marshal JSON body: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to marshal JSON body: %w", err)) } if err = ctx.Err(); err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("aborted because context was done: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) } req, err := newRequest(ctx, url, bytes.NewBuffer(reqBody), "application/json") if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("could not build request: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("could not build request: %w", err)) } resp, err := client.Do(req) if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, err) + return ErrWebrpcRequestFailed.WithCause(err) } defer func() { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) } }() if err = ctx.Err(); err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("aborted because context was done: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) } if resp.StatusCode != 200 { respBody, err := io.ReadAll(resp.Body) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to read server error response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read server error response body: %w", err)) } var rpcErr WebRPCError if err := json.Unmarshal(respBody, &rpcErr); err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to unmarshal server error: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal server error: %w", err)) } if rpcErr.Cause != "" { rpcErr.cause = errors.New(rpcErr.Cause) @@ -562,12 +562,12 @@ func doJSONRequest(ctx context.Context, client HTTPClient, url string, in, out i if out != nil { respBody, err := io.ReadAll(resp.Body) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to read response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read response body: %w", err)) } err = json.Unmarshal(respBody, &out) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to unmarshal JSON response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal JSON response body: %w", err)) } } @@ -715,7 +715,7 @@ func (e WebRPCError) Unwrap() error { return e.cause } -func (e WebRPCError) WithCause(cause error) error { +func (e WebRPCError) WithCause(cause error) WebRPCError { err := e err.cause = cause err.Cause = cause.Error() @@ -752,42 +752,42 @@ var ( // Legacy errors // -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func Errorf(err legacyError, format string, args ...interface{}) WebRPCError { - return ErrorWithCause(err.WebRPCError, fmt.Errorf(format, args...)) + return err.WebRPCError.WithCause(fmt.Errorf(format, args...)) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use .WithCause() method on WebRPCError. func WrapError(err legacyError, cause error, format string, args ...interface{}) WebRPCError { - return ErrorWithCause(err.WebRPCError, fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) + return err.WebRPCError.WithCause(fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func Failf(format string, args ...interface{}) WebRPCError { return Errorf(ErrFail, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use .WithCause() method on WebRPCError. func WrapFailf(cause error, format string, args ...interface{}) WebRPCError { return WrapError(ErrFail, cause, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorNotFound(format string, args ...interface{}) WebRPCError { return Errorf(ErrNotFound, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorInvalidArgument(argument string, validationMsg string) WebRPCError { return Errorf(ErrInvalidArgument, argument+" "+validationMsg) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorRequiredArgument(argument string) WebRPCError { return ErrorInvalidArgument(argument, "is required") } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorInternal(format string, args ...interface{}) WebRPCError { return Errorf(ErrInternal, format, args...) } diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index be4b50b..bdedc80 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -108,7 +108,7 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { // In case of a panic, serve a HTTP 500 error and then panic. if rr := recover(); rr != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcServerPanic, fmt.Errorf("%v", rr))) + RespondWithError(w, ErrWebrpcServerPanic.WithCause(fmt.Errorf("%v", rr))) panic(rr) } }() @@ -124,14 +124,14 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/rpc/ExampleAPI/Status": handler = s.serveStatusJSON case "/rpc/ExampleAPI/GetUsers": handler = s.serveGetUsersJSON default: - err := ErrorWithCause(ErrWebrpcBadRoute, fmt.Errorf("no handler for path %q", r.URL.Path)) + err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no handler for path %q", r.URL.Path)) RespondWithError(w, err) return } if r.Method != "POST" { w.Header().Add("Allow", "POST") // RFC 9110. - err := ErrorWithCause(ErrWebrpcBadMethod, fmt.Errorf("unsupported method %q (only POST is allowed)", r.Method)) + err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %q (only POST is allowed)", r.Method)) RespondWithError(w, err) return } @@ -146,7 +146,7 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "application/json": handler(ctx, w, r) default: - err := ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("unexpected Content-Type: %q", r.Header.Get("Content-Type"))) + err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unexpected Content-Type: %q", r.Header.Get("Content-Type"))) RespondWithError(w, err) } } @@ -181,7 +181,7 @@ func (s *exampleAPIServer) serveStatusJSON(ctx context.Context, w http.ResponseW }{ret0} respBody, err := json.Marshal(respPayload) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to marshal json response: %w", err))) + RespondWithError(w, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) return } @@ -206,7 +206,7 @@ func (s *exampleAPIServer) serveGetUsersJSON(ctx context.Context, w http.Respons }{ret0, ret1} respBody, err := json.Marshal(respPayload) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to marshal json response: %w", err))) + RespondWithError(w, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) return } @@ -218,7 +218,7 @@ func (s *exampleAPIServer) serveGetUsersJSON(ctx context.Context, w http.Respons func RespondWithError(w http.ResponseWriter, err error) { rpcErr, ok := err.(WebRPCError) if !ok { - rpcErr = ErrorWithCause(ErrWebrpcEndpoint, err) + rpcErr = ErrWebrpcEndpoint.WithCause(err) } w.Header().Set("Content-Type", "application/json") @@ -320,41 +320,41 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType func doJSONRequest(ctx context.Context, client HTTPClient, url string, in, out interface{}) error { reqBody, err := json.Marshal(in) if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("failed to marshal JSON body: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to marshal JSON body: %w", err)) } if err = ctx.Err(); err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("aborted because context was done: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) } req, err := newRequest(ctx, url, bytes.NewBuffer(reqBody), "application/json") if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("could not build request: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("could not build request: %w", err)) } resp, err := client.Do(req) if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, err) + return ErrWebrpcRequestFailed.WithCause(err) } defer func() { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) } }() if err = ctx.Err(); err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("aborted because context was done: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) } if resp.StatusCode != 200 { respBody, err := io.ReadAll(resp.Body) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to read server error response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read server error response body: %w", err)) } var rpcErr WebRPCError if err := json.Unmarshal(respBody, &rpcErr); err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to unmarshal server error: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal server error: %w", err)) } if rpcErr.Cause != "" { rpcErr.cause = errors.New(rpcErr.Cause) @@ -365,12 +365,12 @@ func doJSONRequest(ctx context.Context, client HTTPClient, url string, in, out i if out != nil { respBody, err := io.ReadAll(resp.Body) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to read response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read response body: %w", err)) } err = json.Unmarshal(respBody, &out) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to unmarshal JSON response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal JSON response body: %w", err)) } } @@ -482,7 +482,7 @@ func (e WebRPCError) Unwrap() error { return e.cause } -func (e WebRPCError) WithCause(cause error) error { +func (e WebRPCError) WithCause(cause error) WebRPCError { err := e err.cause = cause err.Cause = cause.Error() @@ -510,42 +510,42 @@ var ( // Legacy errors // -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func Errorf(err legacyError, format string, args ...interface{}) WebRPCError { - return ErrorWithCause(err.WebRPCError, fmt.Errorf(format, args...)) + return err.WebRPCError.WithCause(fmt.Errorf(format, args...)) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use .WithCause() method on WebRPCError. func WrapError(err legacyError, cause error, format string, args ...interface{}) WebRPCError { - return ErrorWithCause(err.WebRPCError, fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) + return err.WebRPCError.WithCause(fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func Failf(format string, args ...interface{}) WebRPCError { return Errorf(ErrFail, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use .WithCause() method on WebRPCError. func WrapFailf(cause error, format string, args ...interface{}) WebRPCError { return WrapError(ErrFail, cause, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorNotFound(format string, args ...interface{}) WebRPCError { return Errorf(ErrNotFound, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorInvalidArgument(argument string, validationMsg string) WebRPCError { return Errorf(ErrInvalidArgument, argument+" "+validationMsg) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorRequiredArgument(argument string) WebRPCError { return ErrorInvalidArgument(argument, "is required") } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorInternal(format string, args ...interface{}) WebRPCError { return Errorf(ErrInternal, format, args...) } diff --git a/client.go.tmpl b/client.go.tmpl index 95e8655..9d4ddc9 100644 --- a/client.go.tmpl +++ b/client.go.tmpl @@ -104,41 +104,41 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType func doJSONRequest(ctx context.Context, client HTTPClient, url string, in, out interface{}) error { reqBody, err := json.Marshal(in) if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("failed to marshal JSON body: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to marshal JSON body: %w", err)) } if err = ctx.Err(); err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("aborted because context was done: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) } req, err := newRequest(ctx, url, bytes.NewBuffer(reqBody), "application/json") if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("could not build request: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("could not build request: %w", err)) } resp, err := client.Do(req) if err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, err) + return ErrWebrpcRequestFailed.WithCause(err) } defer func() { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) } }() if err = ctx.Err(); err != nil { - return ErrorWithCause(ErrWebrpcRequestFailed, fmt.Errorf("aborted because context was done: %w", err)) + return ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) } if resp.StatusCode != 200 { respBody, err := io.ReadAll(resp.Body) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to read server error response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read server error response body: %w", err)) } var rpcErr WebRPCError if err := json.Unmarshal(respBody, &rpcErr); err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to unmarshal server error: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal server error: %w", err)) } if rpcErr.Cause != "" { rpcErr.cause = errors.New(rpcErr.Cause) @@ -149,12 +149,12 @@ func doJSONRequest(ctx context.Context, client HTTPClient, url string, in, out i if out != nil { respBody, err := io.ReadAll(resp.Body) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to read response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read response body: %w", err)) } err = json.Unmarshal(respBody, &out) if err != nil { - return ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to unmarshal JSON response body: %w", err)) + return ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal JSON response body: %w", err)) } } diff --git a/errors.go.tmpl b/errors.go.tmpl index ea94bc5..2cae09b 100644 --- a/errors.go.tmpl +++ b/errors.go.tmpl @@ -45,7 +45,7 @@ func (e WebRPCError) Unwrap() error { return e.cause } -func (e WebRPCError) WithCause(cause error) error { +func (e WebRPCError) WithCause(cause error) WebRPCError { err := e err.cause = cause err.Cause = cause.Error() diff --git a/errors.legacy.go.tmpl b/errors.legacy.go.tmpl index d105715..88fadd4 100644 --- a/errors.legacy.go.tmpl +++ b/errors.legacy.go.tmpl @@ -3,42 +3,42 @@ // Legacy errors // -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func Errorf(err legacyError, format string, args ...interface{}) WebRPCError { - return ErrorWithCause(err.WebRPCError, fmt.Errorf(format, args...)) + return err.WebRPCError.WithCause(fmt.Errorf(format, args...)) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use .WithCause() method on WebRPCError. func WrapError(err legacyError, cause error, format string, args ...interface{}) WebRPCError { - return ErrorWithCause(err.WebRPCError, fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) + return err.WebRPCError.WithCause(fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func Failf(format string, args ...interface{}) WebRPCError { return Errorf(ErrFail, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use .WithCause() method on WebRPCError. func WrapFailf(cause error, format string, args ...interface{}) WebRPCError { return WrapError(ErrFail, cause, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorNotFound(format string, args ...interface{}) WebRPCError { return Errorf(ErrNotFound, format, args...) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorInvalidArgument(argument string, validationMsg string) WebRPCError { return Errorf(ErrInvalidArgument, argument+" "+validationMsg) } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorRequiredArgument(argument string) WebRPCError { return ErrorInvalidArgument(argument, "is required") } -// Deprecated: Use ErrorWithCause() instead. +// Deprecated: Use fmt.Errorf() or WebRPCError. func ErrorInternal(format string, args ...interface{}) WebRPCError { return Errorf(ErrInternal, format, args...) } diff --git a/server.go.tmpl b/server.go.tmpl index bd2049e..e8076b3 100644 --- a/server.go.tmpl +++ b/server.go.tmpl @@ -30,7 +30,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer func() { // In case of a panic, serve a HTTP 500 error and then panic. if rr := recover(); rr != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcServerPanic, fmt.Errorf("%v", rr))) + RespondWithError(w, ErrWebrpcServerPanic.WithCause(fmt.Errorf("%v", rr))) panic(rr) } }() @@ -46,14 +46,14 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/rpc/{{$name}}/{{.Name}}": handler = s.serve{{.Name | firstLetterToUpper}}JSON {{- end}} default: - err := ErrorWithCause(ErrWebrpcBadRoute, fmt.Errorf("no handler for path %q", r.URL.Path)) + err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no handler for path %q", r.URL.Path)) RespondWithError(w, err) return } if r.Method != "POST" { w.Header().Add("Allow", "POST") // RFC 9110. - err := ErrorWithCause(ErrWebrpcBadMethod, fmt.Errorf("unsupported method %q (only POST is allowed)", r.Method)) + err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %q (only POST is allowed)", r.Method)) RespondWithError(w, err) return } @@ -68,7 +68,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "application/json": handler(ctx, w, r) default: - err := ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("unexpected Content-Type: %q", r.Header.Get("Content-Type"))) + err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unexpected Content-Type: %q", r.Header.Get("Content-Type"))) RespondWithError(w, err) } } @@ -79,7 +79,7 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context {{ if gt (len .Inputs) 0 -}} reqBody, err := io.ReadAll(r.Body) if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to read request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) return } defer r.Body.Close() @@ -90,7 +90,7 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context {{- end}} }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadRequest, fmt.Errorf("failed to unmarshal request data: %w", err))) + RespondWithError(w, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) return } @@ -119,7 +119,7 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context respBody, err := json.Marshal(respPayload) {{ end -}} if err != nil { - RespondWithError(w, ErrorWithCause(ErrWebrpcBadResponse, fmt.Errorf("failed to marshal json response: %w", err))) + RespondWithError(w, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) return } {{- end}} @@ -139,7 +139,7 @@ func (s *{{$serviceName}}) serve{{ .Name | firstLetterToUpper }}JSON(ctx context func RespondWithError(w http.ResponseWriter, err error) { rpcErr, ok := err.(WebRPCError) if !ok { - rpcErr = ErrorWithCause(ErrWebrpcEndpoint, err) + rpcErr = ErrWebrpcEndpoint.WithCause(err) } w.Header().Set("Content-Type", "application/json")