-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(errors): augment errors with new "Problem" types (#199)
This commit adds new "Problem" types for standardizing error objects and enriching them with additional information to help consumers of this package (and downstream consumers of tools built with this package) understand and respond to their problematic scenarios. Problem scenarios are also identifiable via a hash ID computed from the stable fields of the problem instance. The different flavors of problem types (SDK and HTTP) have unique fields, tailored to the context they occur in, in additional to the shared fields common across all problem types. This commit will enable downstream tools to begin supporting new error formats and using them to empower their users to fix their issues. Utilizing the Golang `error` interface, this change is completely compatible with previous behavior. Consumers only take advantage of the new details if they choose to. Downstream libraries should be able to include this code in their dependencies without breaking. Signed-off-by: Dustin Popp <[email protected]>
- Loading branch information
Showing
38 changed files
with
2,462 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package core | ||
|
||
// (C) Copyright IBM Corp. 2024. | ||
// | ||
// 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. | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// AuthenticationError describes the problem returned when | ||
// authentication over HTTP fails. | ||
type AuthenticationError struct { | ||
Err error | ||
*HTTPProblem | ||
} | ||
|
||
// NewAuthenticationError is a deprecated function that was previously used for creating new | ||
// AuthenticationError structs. HTTPProblem types should be used instead of AuthenticationError types. | ||
func NewAuthenticationError(response *DetailedResponse, err error) *AuthenticationError { | ||
GetLogger().Warn("NewAuthenticationError is deprecated and should not be used.") | ||
authError := authenticationErrorf(err, response, "unknown", NewProblemComponent("unknown", "unknown")) | ||
return authError | ||
} | ||
|
||
// authenticationErrorf creates and returns a new instance of "AuthenticationError". | ||
func authenticationErrorf(err error, response *DetailedResponse, operationID string, component *ProblemComponent) *AuthenticationError { | ||
// This function should always be called with non-nil error instances. | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
var httpErr *HTTPProblem | ||
if !errors.As(err, &httpErr) { | ||
if response == nil { | ||
return nil | ||
} | ||
httpErr = httpErrorf(err.Error(), response) | ||
} | ||
|
||
enrichHTTPProblem(httpErr, operationID, component) | ||
|
||
return &AuthenticationError{ | ||
HTTPProblem: httpErr, | ||
Err: err, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package core | ||
|
||
// (C) Copyright IBM Corp. 2024. | ||
// | ||
// 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. | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEmbedsHTTPProblem(t *testing.T) { | ||
authErr := &AuthenticationError{ | ||
Err: errors.New(""), | ||
HTTPProblem: &HTTPProblem{ | ||
OperationID: "", | ||
Response: &DetailedResponse{}, | ||
}, | ||
} | ||
|
||
assert.NotNil(t, authErr.GetConsoleMessage) | ||
assert.NotNil(t, authErr.GetDebugMessage) | ||
assert.NotNil(t, authErr.GetID) | ||
assert.NotNil(t, authErr.getErrorCode) | ||
assert.NotNil(t, authErr.getHeader) | ||
assert.NotNil(t, authErr.GetConsoleOrderedMaps) | ||
assert.NotNil(t, authErr.GetDebugOrderedMaps) | ||
} | ||
|
||
func TestNewAuthenticationError(t *testing.T) { | ||
unknown := "unknown" | ||
err := errors.New("test") | ||
resp := getMockAuthResponse() | ||
|
||
authErr := NewAuthenticationError(resp, err) | ||
|
||
assert.NotNil(t, authErr) | ||
assert.Equal(t, err, authErr.Err) | ||
assert.Equal(t, resp, authErr.Response) | ||
assert.Equal(t, "test", authErr.Summary) | ||
assert.Equal(t, unknown, authErr.OperationID) | ||
assert.Equal(t, unknown, authErr.Component.Name) | ||
assert.Equal(t, unknown, authErr.Component.Version) | ||
} | ||
|
||
func TestAuthenticationErrorfHTTPProblem(t *testing.T) { | ||
resp := getMockAuthResponse() | ||
httpProb := httpErrorf("Unauthorized", resp) | ||
assert.Empty(t, httpProb.OperationID) | ||
assert.Empty(t, httpProb.Component) | ||
|
||
authErr := authenticationErrorf(httpProb, nil, "get_token", NewProblemComponent("iam", "v1")) | ||
assert.Equal(t, httpProb, authErr.Err) | ||
assert.Equal(t, resp, authErr.Response) | ||
assert.Equal(t, "Unauthorized", authErr.Summary) | ||
assert.Equal(t, "get_token", authErr.OperationID) | ||
assert.Equal(t, "iam", authErr.Component.Name) | ||
assert.Equal(t, "v1", authErr.Component.Version) | ||
} | ||
|
||
func TestAuthenticationErrorfOtherError(t *testing.T) { | ||
err := errors.New("test") | ||
resp := getMockAuthResponse() | ||
|
||
authErr := authenticationErrorf(err, resp, "get_token", NewProblemComponent("iam", "v1")) | ||
assert.NotNil(t, authErr) | ||
assert.Equal(t, err, authErr.Err) | ||
assert.Equal(t, resp, authErr.Response) | ||
assert.Equal(t, "test", authErr.Summary) | ||
assert.Equal(t, "get_token", authErr.OperationID) | ||
assert.Equal(t, "iam", authErr.Component.Name) | ||
assert.Equal(t, "v1", authErr.Component.Version) | ||
} | ||
|
||
func TestAuthenticationErrorfNoErr(t *testing.T) { | ||
authErr := authenticationErrorf(nil, getMockAuthResponse(), "get_token", NewProblemComponent("iam", "v1")) | ||
assert.Nil(t, authErr) | ||
} | ||
|
||
func TestAuthenticationErrorfNoResponse(t *testing.T) { | ||
authErr := authenticationErrorf(errors.New("not http, needs response"), nil, "get_token", NewProblemComponent("iam", "v1")) | ||
assert.Nil(t, authErr) | ||
} | ||
|
||
func getMockAuthResponse() *DetailedResponse { | ||
return &DetailedResponse{ | ||
StatusCode: 401, | ||
Result: "Unauthorized", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.