@@ -12,6 +12,7 @@ import (
1212 "github.com/conductorone/baton-sdk/pkg/uhttp"
1313 "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1414 "go.uber.org/zap"
15+ "google.golang.org/grpc/codes"
1516)
1617
1718var (
@@ -32,22 +33,42 @@ func (c *WorkatoClient) getPath(path string) *url.URL {
3233 return c .baseUrl .JoinPath (path )
3334}
3435
36+ // httpToGRPCCode maps HTTP status codes to gRPC codes.
37+ func httpToGRPCCode (statusCode int ) codes.Code {
38+ switch statusCode {
39+ case http .StatusBadRequest :
40+ return codes .InvalidArgument
41+ case http .StatusUnauthorized :
42+ return codes .Unauthenticated
43+ case http .StatusForbidden :
44+ return codes .PermissionDenied
45+ case http .StatusNotFound :
46+ return codes .NotFound
47+ case http .StatusInternalServerError :
48+ return codes .Internal
49+ default :
50+ return codes .Unknown
51+ }
52+ }
53+
3554func getError (ctx context.Context , originalErr error , resp * http.Response ) error {
55+ grpcCode := httpToGRPCCode (resp .StatusCode )
56+
3657 bytes , err := io .ReadAll (resp .Body )
3758 if err != nil {
3859 // We expect the response body to be JSON, according to the Workato API docs, but this is not guaranteed.
3960 l := ctxzap .Extract (ctx )
4061 l .Debug ("failed to read response body" , zap .String ("body" , string (bytes )))
41- return errors .Join (originalErr , err )
62+ return uhttp . WrapErrors ( grpcCode , "baton-workato: failed to read error response" , errors .Join (originalErr , err ) )
4263 }
4364
4465 var cErr ApiError
4566 err = json .Unmarshal (bytes , & cErr )
4667 if err != nil {
47- return errors .Join (originalErr , err )
68+ return uhttp . WrapErrors ( grpcCode , "baton-workato: failed to parse error response" , errors .Join (originalErr , err ) )
4869 }
4970
50- return errors .Join (originalErr , errors .New (cErr .Message ))
71+ return uhttp . WrapErrors ( grpcCode , fmt . Sprintf ( "baton-workato: API error (status %d)" , resp . StatusCode ), errors .Join (originalErr , errors .New (cErr .Message ) ))
5172}
5273
5374func (c * WorkatoClient ) doRequest (ctx context.Context , method string , urlAddress * url.URL , res interface {}, body interface {}) error {
@@ -64,7 +85,7 @@ func (c *WorkatoClient) doRequest(ctx context.Context, method string, urlAddress
6485 uhttp .WithJSONBody (body ),
6586 )
6687 if err != nil {
67- return err
88+ return uhttp . WrapErrors ( codes . Internal , "baton-workato: failed to create HTTP request" , err )
6889 }
6990
7091 var options []uhttp.DoOption
@@ -77,10 +98,10 @@ func (c *WorkatoClient) doRequest(ctx context.Context, method string, urlAddress
7798
7899 if resp == nil {
79100 if err != nil {
80- return err
101+ return uhttp . WrapErrors ( codes . Internal , "baton-workato: response is nil and error is nil, this should never happen" , err )
81102 }
82103
83- return errors . New ( "baton-workato: response is nil and error is nil, this should never happen, might be a bug in the http client" )
104+ return uhttp . WrapErrors ( codes . Internal , "baton-workato: response is nil and error is nil, this should never happen" , err )
84105 }
85106
86107 defer resp .Body .Close ()
0 commit comments