Skip to content
Merged
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
36 changes: 36 additions & 0 deletions pkg/httperr/default_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package httperr

import (
"encoding/json"
"fmt"
"net/http"

"github.com/mittwald/api-client-go/mittwaldv2/generated/schemas/commonsv2"
)

func IsDefaultErrorResponse(res *http.Response) (*commonsv2.Error, bool) {
target := commonsv2.Error{}

if err := json.NewDecoder(res.Body).Decode(&target); err != nil {
return nil, false
}
Comment thread
tillwestpfahl marked this conversation as resolved.

if target.Type == "" || target.Message == "" {
return nil, false
}

return &target, true
}

// ErrDefault represents the default fallback error used in the mittwald api.
type ErrDefault struct {
Response *http.Response
DefaultError *commonsv2.Error
}

func (e *ErrDefault) Error() string {
if e.DefaultError != nil {
return fmt.Sprintf("%s: %s", e.DefaultError.Type, e.DefaultError.Message)
}
return "unexpected default error: <nil>"
}
4 changes: 4 additions & 0 deletions pkg/httperr/from_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func ErrFromResponse(res *http.Response) error {
}
return &ErrBadRequest{Response: res}
default:
if defaultError, isDefaultError := IsDefaultErrorResponse(res); isDefaultError {
return &ErrDefault{Response: res, DefaultError: defaultError}
}

return &ErrUnexpectedResponse{Response: res}
}
}