Skip to content

Commit

Permalink
feat(errors): augment errors with new "Problem" types (#199)
Browse files Browse the repository at this point in the history
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
dpopp07 authored Mar 13, 2024
1 parent 78b4cd0 commit 6ec86dd
Show file tree
Hide file tree
Showing 38 changed files with 2,462 additions and 304 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ COV_OPTS=-coverprofile=coverage.txt -covermode=atomic

all: tidy test lint

build:
${GO} build ./...

testcov:
${GO} test -tags=all ${COV_OPTS} ./...

Expand Down
57 changes: 57 additions & 0 deletions core/authentication_error.go
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,
}
}
102 changes: 102 additions & 0 deletions core/authentication_error_test.go
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",
}
}
17 changes: 0 additions & 17 deletions core/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,3 @@ type Authenticator interface {
Authenticate(*http.Request) error
Validate() error
}

// AuthenticationError describes the error returned when authentication fails
type AuthenticationError struct {
Response *DetailedResponse
Err error
}

func (e *AuthenticationError) Error() string {
return e.Err.Error()
}

func NewAuthenticationError(response *DetailedResponse, err error) *AuthenticationError {
return &AuthenticationError{
Response: response,
Err: err,
}
}
7 changes: 6 additions & 1 deletion core/authenticator_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ func GetAuthenticatorFromEnvironment(credentialKey string) (authenticator Authen
} else if strings.EqualFold(authType, AUTHTYPE_NOAUTH) {
authenticator, err = NewNoAuthAuthenticator()
} else {
err = fmt.Errorf(ERRORMSG_AUTHTYPE_UNKNOWN, authType)
err = SDKErrorf(
nil,
fmt.Sprintf(ERRORMSG_AUTHTYPE_UNKNOWN, authType),
"unknown-auth-type",
getComponentInfo(),
)
}

return
Expand Down
Loading

0 comments on commit 6ec86dd

Please sign in to comment.