Skip to content
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

Prototype the new ConjureErrorDecoder #716

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions conjure-go-client/httpclient/request_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/palantir/conjure-go-runtime/v2/conjure-go-contract/codecs"
"github.com/palantir/conjure-go-runtime/v2/conjure-go-contract/errors"
werror "github.com/palantir/witchcraft-go-error"
)

Expand Down Expand Up @@ -230,3 +231,14 @@ func WithRequestTimeout(timeout time.Duration) RequestParam {
return nil
})
}

func WithRequestConjureErrorDecoder(ced errors.ConjureErrorDecoder) RequestParam {
return requestParamFunc(func(b *requestBuilder) error {
b.errorDecoderMiddleware = errorDecoderMiddleware{
errorDecoder: restErrorDecoder{
conjureErrorDecoder: ced,
},
}
return nil
})
}
12 changes: 10 additions & 2 deletions conjure-go-client/httpclient/response_error_decoder_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func (e errorDecoderMiddleware) RoundTrip(req *http.Request, next http.RoundTrip
// If the response has a Content-Type containing 'application/json', we attempt
// to unmarshal the error as a conjure error. See TestErrorDecoderMiddlewares for
// example error messages and parameters.
type restErrorDecoder struct{}
type restErrorDecoder struct {
conjureErrorDecoder errors.ConjureErrorDecoder
}

var _ ErrorDecoder = restErrorDecoder{}

Expand Down Expand Up @@ -102,7 +104,13 @@ func (d restErrorDecoder) DecodeError(resp *http.Response) error {
if isJSON := strings.Contains(resp.Header.Get("Content-Type"), codecs.JSON.ContentType()); !isJSON {
return werror.Error(resp.Status, wSafeParams, wUnsafeParams, werror.UnsafeParam("responseBody", string(body)))
}
conjureErr, jsonErr := errors.UnmarshalError(body)
var conjureErr errors.Error
var jsonErr error
if d.conjureErrorDecoder != nil {
conjureErr, jsonErr = errors.UnmarshalErrorWithDecoder(d.conjureErrorDecoder, body)
} else {
conjureErr, jsonErr = errors.UnmarshalError(body)
}
if jsonErr != nil {
return werror.Error(resp.Status, wSafeParams, wUnsafeParams, werror.UnsafeParam("responseBody", string(body)))
}
Expand Down
19 changes: 19 additions & 0 deletions conjure-go-contract/errors/conjure_error_decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2024 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package errors

type ConjureErrorDecoder interface {
DecodeConjureError(name string, body []byte) (Error, error)
}
16 changes: 16 additions & 0 deletions conjure-go-contract/errors/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ func UnmarshalError(body []byte) (Error, error) {
// Cast should never panic, as we've verified in RegisterErrorType
return instance.(Error), nil
}

// UnmarshalErrorWithDecoder attempts to deserialize the message to a known implementation of Error
// using the provided ConjureErrorDecoder.
func UnmarshalErrorWithDecoder(ced ConjureErrorDecoder, body []byte) (Error, error) {
var name struct {
Name string `json:"errorName"`
}
if err := codecs.JSON.Unmarshal(body, &name); err != nil {
return nil, werror.Wrap(err, "failed to unmarshal body as conjure error")
}
cErr, err := ced.DecodeConjureError(name.Name, body)
if err != nil {
return nil, werror.Wrap(err, "failed to decode body using ConjureErrorDecoder")
}
return cErr, nil
}