-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HMS-1668 refactor: Structured HTTP error (nil arg)
Add helpers to create an HTTP Error from a format string and an optional internal error object. Replace nil arg checks with new helper `NilArgError(name)`. Signed-off-by: Christian Heimes <[email protected]>
- Loading branch information
Showing
19 changed files
with
177 additions
and
92 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
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,31 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
// NewHTTPErrorWithInternal creates a new HTTPError instance from a format | ||
// string and an error object. | ||
func NewHTTPErrorWithInternal(internal error, code int, format string, a ...any) error { | ||
var msg string | ||
if len(a) != 0 { | ||
msg = fmt.Sprintf(format, a...) | ||
} else { | ||
msg = format | ||
} | ||
return &echo.HTTPError{Code: code, Message: msg, Internal: internal} | ||
} | ||
|
||
// NewHTTPErrorF creates a new HTTPError instance from a format string. | ||
func NewHTTPErrorF(code int, format string, a ...any) error { | ||
return NewHTTPErrorWithInternal(nil, code, format, a...) | ||
} | ||
|
||
// NilArgError creates a new HTTPError instance with "'name' cannot be nil" | ||
// error message. | ||
func NilArgError(name string) error { | ||
return NewHTTPErrorF(http.StatusInternalServerError, "'%s' cannot be nil", name) | ||
} |
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,40 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func httpErrorFromErr(t *testing.T, err error) (he *echo.HTTPError) { | ||
assert.Error(t, err) | ||
he, ok := err.(*echo.HTTPError) | ||
assert.True(t, ok) | ||
return he | ||
} | ||
|
||
func TestNewHTTPError(t *testing.T) { | ||
internal := errors.New("internal error") | ||
err := NewHTTPErrorWithInternal(internal, http.StatusForbidden, "forbidden %s!", "resource") | ||
he := httpErrorFromErr(t, err) | ||
assert.Equal(t, he.Code, http.StatusForbidden) | ||
assert.Equal(t, he.Message, "forbidden resource!") | ||
assert.ErrorIs(t, he.Internal, internal) | ||
|
||
err = NewHTTPErrorWithInternal(internal, http.StatusForbidden, "forbidden") | ||
he = httpErrorFromErr(t, err) | ||
assert.Equal(t, he.Code, http.StatusForbidden) | ||
assert.Equal(t, he.Message, "forbidden") | ||
assert.ErrorIs(t, he.Internal, internal) | ||
} | ||
|
||
func TestNilArgError(t *testing.T) { | ||
err := NilArgError("param") | ||
he := httpErrorFromErr(t, err) | ||
assert.Equal(t, he.Code, http.StatusInternalServerError) | ||
assert.Equal(t, he.Message, "'param' cannot be nil") | ||
assert.Nil(t, he.Internal) | ||
} |
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
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.