From 5275eca505251b21354e257d0e33092ff86b88fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20-=20=E3=82=A2=E3=83=AC=E3=83=83=E3=82=AF=E3=82=B9?= Date: Thu, 17 Oct 2024 16:29:29 +0200 Subject: [PATCH] Replace WithCause with WithCausef (#73) * Replace WithCause with WithCausef * use head --- _examples/golang-basics/example.gen.go | 60 +++++++++++++------------- _examples/golang-imports/api.gen.go | 38 ++++++++-------- client.go.tmpl | 18 ++++---- errors.legacy.go.tmpl | 4 +- server.go.tmpl | 22 +++++----- 5 files changed, 71 insertions(+), 71 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 204d609..c807af7 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,6 +1,6 @@ // example v0.0.1 05b7a5c86b98738f4fe6ce9bb1fccd4af064847a // -- -// Code generated by webrpc-gen@v0.19.3-1-g8d197ff with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.20.3-1-gf6584bc with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -legacyErrors -fixEmptyArrays -out=./example.gen.go package main @@ -262,7 +262,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 { - s.sendErrorJSON(w, r, ErrWebrpcServerPanic.WithCause(fmt.Errorf("%v", rr))) + s.sendErrorJSON(w, r, ErrWebrpcServerPanic.WithCausef("%v", rr)) panic(rr) } }() @@ -289,14 +289,14 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) case "/rpc/ExampleService/LogEvent": handler = s.serveLogEventJSON default: - err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no WebRPC method defined for path %v", r.URL.Path)) + err := ErrWebrpcBadRoute.WithCausef("no WebRPC method defined for path %v", r.URL.Path) s.sendErrorJSON(w, r, err) return } if r.Method != "POST" { w.Header().Add("Allow", "POST") // RFC 9110. - err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %v (only POST is allowed)", r.Method)) + err := ErrWebrpcBadMethod.WithCausef("unsupported method %v (only POST is allowed)", r.Method) s.sendErrorJSON(w, r, err) return } @@ -322,7 +322,7 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) handler(ctx, w, r) default: - err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type"))) + err := ErrWebrpcBadRequest.WithCausef("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type")) s.sendErrorJSON(w, r, err) } } @@ -365,7 +365,7 @@ func (s *exampleServiceServer) serveStatusJSON(ctx context.Context, w http.Respo }{ret0} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err)) return } @@ -393,7 +393,7 @@ func (s *exampleServiceServer) serveVersionJSON(ctx context.Context, w http.Resp }{ret0} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err)) return } @@ -407,7 +407,7 @@ func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.Resp reqBody, err := io.ReadAll(r.Body) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to read request data: %w", err)) return } defer r.Body.Close() @@ -417,7 +417,7 @@ func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.Resp Arg1 uint64 `json:"userID"` }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to unmarshal request data: %w", err)) return } @@ -437,7 +437,7 @@ func (s *exampleServiceServer) serveGetUserJSON(ctx context.Context, w http.Resp }{ret0} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err)) return } @@ -451,7 +451,7 @@ func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.Res reqBody, err := io.ReadAll(r.Body) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to read request data: %w", err)) return } defer r.Body.Close() @@ -460,7 +460,7 @@ func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.Res Arg0 *SearchFilter `json:"s"` }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to unmarshal request data: %w", err)) return } @@ -481,7 +481,7 @@ func (s *exampleServiceServer) serveFindUserJSON(ctx context.Context, w http.Res }{ret0, ret1} respBody, err := json.Marshal(initializeNilSlices(respPayload)) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err)) return } @@ -495,7 +495,7 @@ func (s *exampleServiceServer) serveLogEventJSON(ctx context.Context, w http.Res reqBody, err := io.ReadAll(r.Body) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to read request data: %w", err)) return } defer r.Body.Close() @@ -504,7 +504,7 @@ func (s *exampleServiceServer) serveLogEventJSON(ctx context.Context, w http.Res Arg0 string `json:"event"` }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to unmarshal request data: %w", err)) return } @@ -582,7 +582,7 @@ func (c *exampleServiceClient) Ping(ctx context.Context) error { if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -598,7 +598,7 @@ func (c *exampleServiceClient) Status(ctx context.Context) (bool, error) { if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -614,7 +614,7 @@ func (c *exampleServiceClient) Version(ctx context.Context) (*Version, error) { if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -634,7 +634,7 @@ func (c *exampleServiceClient) GetUser(ctx context.Context, header map[string]st if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -654,7 +654,7 @@ func (c *exampleServiceClient) FindUser(ctx context.Context, s *SearchFilter) (s if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -670,7 +670,7 @@ func (c *exampleServiceClient) LogEvent(ctx context.Context, event string) error if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -721,15 +721,15 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out interface{}) (*http.Response, error) { reqBody, err := json.Marshal(in) if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to marshal JSON body: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("failed to marshal JSON body: %w", err) } if err = ctx.Err(); err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("aborted because context was done: %w", err) } req, err := newRequest(ctx, url, bytes.NewBuffer(reqBody), "application/json") if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("could not build request: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("could not build request: %w", err) } resp, err := client.Do(req) @@ -740,12 +740,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if resp.StatusCode != 200 { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read server error response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read server error response body: %w", err) } var rpcErr WebRPCError if err := json.Unmarshal(respBody, &rpcErr); err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal server error: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal server error: %w", err) } if rpcErr.Cause != "" { rpcErr.cause = errors.New(rpcErr.Cause) @@ -756,12 +756,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if out != nil { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read response body: %w", err) } err = json.Unmarshal(respBody, &out) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal JSON response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal JSON response body: %w", err) } } @@ -983,12 +983,12 @@ var ( // Deprecated: Use fmt.Errorf() or WebRPCError. func Errorf(err legacyError, format string, args ...interface{}) WebRPCError { - return err.WebRPCError.WithCause(fmt.Errorf(format, args...)) + return err.WebRPCError.WithCausef(format, args...) } // Deprecated: Use .WithCause() method on WebRPCError. func WrapError(err legacyError, cause error, format string, args ...interface{}) WebRPCError { - return err.WebRPCError.WithCause(fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) + return err.WebRPCError.WithCausef("%v: %w", fmt.Errorf(format, args...), cause) } // Deprecated: Use fmt.Errorf() or WebRPCError. diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index ef953a8..4b58c54 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -1,6 +1,6 @@ // example-api-service v1.0.0 cae4e128f4fb4c938bfe1ea312deeea3dfd6b6af // -- -// Code generated by webrpc-gen@v0.19.3-1-g8d197ff with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.20.3-1-gf6584bc with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=./proto/api.ridl -target=../../../gen-golang -out=./api.gen.go -pkg=main -server -client -legacyErrors=true -fmt=false package main @@ -162,7 +162,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 { - s.sendErrorJSON(w, r, ErrWebrpcServerPanic.WithCause(fmt.Errorf("%v", rr))) + s.sendErrorJSON(w, r, ErrWebrpcServerPanic.WithCausef("%v", rr)) panic(rr) } }() @@ -183,14 +183,14 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/rpc/ExampleAPI/GetUsers": handler = s.serveGetUsersJSON default: - err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no WebRPC method defined for path %v", r.URL.Path)) + err := ErrWebrpcBadRoute.WithCausef("no WebRPC method defined for path %v", r.URL.Path) s.sendErrorJSON(w, r, err) return } if r.Method != "POST" { w.Header().Add("Allow", "POST") // RFC 9110. - err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %v (only POST is allowed)", r.Method)) + err := ErrWebrpcBadMethod.WithCausef("unsupported method %v (only POST is allowed)", r.Method) s.sendErrorJSON(w, r, err) return } @@ -216,7 +216,7 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { handler(ctx, w, r) default: - err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type"))) + err := ErrWebrpcBadRequest.WithCausef("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type")) s.sendErrorJSON(w, r, err) } } @@ -263,7 +263,7 @@ func (s *exampleAPIServer) serveStatusJSON(ctx context.Context, w http.ResponseW }{ret0} respBody, err := json.Marshal(respPayload) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err)) return } @@ -294,7 +294,7 @@ func (s *exampleAPIServer) serveGetUsersJSON(ctx context.Context, w http.Respons }{ret0, ret1} respBody, err := json.Marshal(respPayload) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err)) return } @@ -363,7 +363,7 @@ func (c *exampleAPIClient) Ping(ctx context.Context) error { if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -379,7 +379,7 @@ func (c *exampleAPIClient) Status(ctx context.Context) (bool, error) { if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -396,7 +396,7 @@ func (c *exampleAPIClient) GetUsers(ctx context.Context) ([]*User, Location, err if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -447,15 +447,15 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out interface{}) (*http.Response, error) { reqBody, err := json.Marshal(in) if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to marshal JSON body: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("failed to marshal JSON body: %w", err) } if err = ctx.Err(); err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("aborted because context was done: %w", err) } req, err := newRequest(ctx, url, bytes.NewBuffer(reqBody), "application/json") if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("could not build request: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("could not build request: %w", err) } resp, err := client.Do(req) @@ -466,12 +466,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if resp.StatusCode != 200 { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read server error response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read server error response body: %w", err) } var rpcErr WebRPCError if err := json.Unmarshal(respBody, &rpcErr); err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal server error: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal server error: %w", err) } if rpcErr.Cause != "" { rpcErr.cause = errors.New(rpcErr.Cause) @@ -482,12 +482,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if out != nil { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read response body: %w", err) } err = json.Unmarshal(respBody, &out) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal JSON response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal JSON response body: %w", err) } } @@ -664,12 +664,12 @@ var ( // Deprecated: Use fmt.Errorf() or WebRPCError. func Errorf(err legacyError, format string, args ...interface{}) WebRPCError { - return err.WebRPCError.WithCause(fmt.Errorf(format, args...)) + return err.WebRPCError.WithCausef(format, args...) } // Deprecated: Use .WithCause() method on WebRPCError. func WrapError(err legacyError, cause error, format string, args ...interface{}) WebRPCError { - return err.WebRPCError.WithCause(fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) + return err.WebRPCError.WithCausef("%v: %w", fmt.Errorf(format, args...), cause) } // Deprecated: Use fmt.Errorf() or WebRPCError. diff --git a/client.go.tmpl b/client.go.tmpl index 23f14d9..6d1be1d 100644 --- a/client.go.tmpl +++ b/client.go.tmpl @@ -63,7 +63,7 @@ func (c *{{$serviceNameClient}}) {{$method.Name}}(ctx context.Context{{range $_, if resp != nil { cerr := resp.Body.Close() if err == nil && cerr != nil { - err = ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to close response body: %w", cerr)) + err = ErrWebrpcRequestFailed.WithCausef("failed to close response body: %w", cerr) } } @@ -173,7 +173,7 @@ func (r *streamReader) handleReadError(err error) error { if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded){ return ErrWebrpcClientDisconnected.WithCause(err) } - return ErrWebrpcBadResponse.WithCause(fmt.Errorf("reading stream: %w", err)) + return ErrWebrpcBadResponse.WithCausef("reading stream: %w", err) } {{- end }} @@ -222,15 +222,15 @@ func newRequest(ctx context.Context, url string, reqBody io.Reader, contentType func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out interface{}) (*http.Response, error) { reqBody, err := json.Marshal(in) if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("failed to marshal JSON body: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("failed to marshal JSON body: %w", err) } if err = ctx.Err(); err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("aborted because context was done: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("aborted because context was done: %w", err) } req, err := newRequest(ctx, url, bytes.NewBuffer(reqBody), "application/json") if err != nil { - return nil, ErrWebrpcRequestFailed.WithCause(fmt.Errorf("could not build request: %w", err)) + return nil, ErrWebrpcRequestFailed.WithCausef("could not build request: %w", err) } resp, err := client.Do(req) @@ -241,12 +241,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if resp.StatusCode != 200 { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read server error response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read server error response body: %w", err) } var rpcErr WebRPCError if err := json.Unmarshal(respBody, &rpcErr); err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal server error: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal server error: %w", err) } if rpcErr.Cause != "" { rpcErr.cause = errors.New(rpcErr.Cause) @@ -257,12 +257,12 @@ func doHTTPRequest(ctx context.Context, client HTTPClient, url string, in, out i if out != nil { respBody, err := io.ReadAll(resp.Body) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to read response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to read response body: %w", err) } err = json.Unmarshal(respBody, &out) if err != nil { - return nil, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to unmarshal JSON response body: %w", err)) + return nil, ErrWebrpcBadResponse.WithCausef("failed to unmarshal JSON response body: %w", err) } } diff --git a/errors.legacy.go.tmpl b/errors.legacy.go.tmpl index 88fadd4..49ca29f 100644 --- a/errors.legacy.go.tmpl +++ b/errors.legacy.go.tmpl @@ -5,12 +5,12 @@ // Deprecated: Use fmt.Errorf() or WebRPCError. func Errorf(err legacyError, format string, args ...interface{}) WebRPCError { - return err.WebRPCError.WithCause(fmt.Errorf(format, args...)) + return err.WebRPCError.WithCausef(format, args...) } // Deprecated: Use .WithCause() method on WebRPCError. func WrapError(err legacyError, cause error, format string, args ...interface{}) WebRPCError { - return err.WebRPCError.WithCause(fmt.Errorf("%v: %w", fmt.Errorf(format, args...), cause)) + return err.WebRPCError.WithCausef("%v: %w", fmt.Errorf(format, args...), cause) } // Deprecated: Use fmt.Errorf() or WebRPCError. diff --git a/server.go.tmpl b/server.go.tmpl index 4bb6a89..49f9751 100644 --- a/server.go.tmpl +++ b/server.go.tmpl @@ -33,7 +33,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 { - s.sendErrorJSON(w, r, ErrWebrpcServerPanic.WithCause(fmt.Errorf("%v", rr))) + s.sendErrorJSON(w, r, ErrWebrpcServerPanic.WithCausef("%v", rr)) panic(rr) } }() @@ -52,14 +52,14 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { handler = s.serve{{$method.Name | firstLetterToUpper}}JSON{{if $method.StreamOutput}}Stream{{end}} {{- end}} default: - err := ErrWebrpcBadRoute.WithCause(fmt.Errorf("no WebRPC method defined for path %v", r.URL.Path)) + err := ErrWebrpcBadRoute.WithCausef("no WebRPC method defined for path %v", r.URL.Path) s.sendErrorJSON(w, r, err) return } if r.Method != "POST" { w.Header().Add("Allow", "POST") // RFC 9110. - err := ErrWebrpcBadMethod.WithCause(fmt.Errorf("unsupported method %v (only POST is allowed)", r.Method)) + err := ErrWebrpcBadMethod.WithCausef("unsupported method %v (only POST is allowed)", r.Method) s.sendErrorJSON(w, r, err) return } @@ -85,7 +85,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { handler(ctx, w, r) default: - err := ErrWebrpcBadRequest.WithCause(fmt.Errorf("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type"))) + err := ErrWebrpcBadRequest.WithCausef("unsupported Content-Type %q (only application/json is allowed)", r.Header.Get("Content-Type")) s.sendErrorJSON(w, r, err) } } @@ -97,7 +97,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSON(ctx cont {{ if gt (len $method.Inputs) 0 -}} reqBody, err := io.ReadAll(r.Body) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to read request data: %w", err)) return } defer r.Body.Close() @@ -108,7 +108,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSON(ctx cont {{- end}} }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to unmarshal request data: %w", err)) return } {{- end }} @@ -141,7 +141,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSON(ctx cont respBody, err := json.Marshal(respPayload) {{ end -}} if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCause(fmt.Errorf("failed to marshal json response: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err)) return } {{- end}} @@ -162,7 +162,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSONStream(ct {{ if gt (len $method.Inputs) 0 -}} reqBody, err := io.ReadAll(r.Body) if err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to read request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to read request data: %w", err)) return } defer r.Body.Close() @@ -173,14 +173,14 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSONStream(ct {{- end}} }{} if err := json.Unmarshal(reqBody, &reqPayload); err != nil { - s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCause(fmt.Errorf("failed to unmarshal request data: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to unmarshal request data: %w", err)) return } {{- end }} f, ok := w.(http.Flusher) if !ok { - s.sendErrorJSON(w, r, ErrWebrpcInternalError.WithCause(fmt.Errorf("server http.ResponseWriter doesn't support .Flush() method"))) + s.sendErrorJSON(w, r, ErrWebrpcInternalError.WithCausef("server http.ResponseWriter doesn't support .Flush() method")) return } @@ -192,7 +192,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSONStream(ct streamWriter := &{{firstLetterToLower $method.Name}}StreamWriter{streamWriter{w: w, f: f, e: json.NewEncoder(w), sendError: s.sendErrorJSON}} if err := streamWriter.ping(); err != nil { - s.sendErrorJSON(w, r, ErrWebrpcStreamLost.WithCause(fmt.Errorf("failed to establish SSE stream: %w", err))) + s.sendErrorJSON(w, r, ErrWebrpcStreamLost.WithCausef("failed to establish SSE stream: %w", err)) return }