Skip to content

Commit ba29500

Browse files
committed
[patch] added dedicated helper function for Internal error type as well
[-] updated readme with more info on helper functions for error types
1 parent 88bf2d2 commit ba29500

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
Errors package is a drop-in replacement of the built-in Go errors package with no external dependencies. It lets you create errors of 11 different types which should handle most of the use cases. Some of them are a bit too specific for web applications, but useful nonetheless. Following are the primary features of this package:
1212

13-
1. Custom error types
13+
1. Multiple (11) error types
1414
2. User friendly message
1515
3. File & line number prefixed to errors
1616

@@ -20,7 +20,7 @@ In case of nested errors, the messages (in case of nesting with this package's e
2020

2121
Go 1.13+
2222

23-
### Custom error types
23+
### Available error types
2424

2525
1. TypeInternal - is the error type for when there is an internal system error. e.g. Database errors
2626
2. TypeValidation - is the error type for when there is a validation error. e.g. invalid email address
@@ -34,6 +34,10 @@ Go 1.13+
3434
10. TypeSubscriptionExpired - is the error type for when a user's 'paid' account has expired
3535
11. TypeDownstreamDependencyTimedout - is the error type for when a request to a downstream dependent service times out
3636

37+
Helper functions are available for all the error types. Each of them have 2 helper functions, one which accepts only a string, and the other which accepts an original error as well as a user friendly message.
38+
39+
All the dedicated error type functions are documented [here](https://pkg.go.dev/github.com/bnkamalesh/errors?tab=doc#DownstreamDependencyTimedout). Names are consistent with the error type, e.g. errors.Internal(string) and errors.InternalErr(error, string)
40+
3741
### User friendly messages
3842

3943
More often than not, when writing APIs, we'd want to respond with an easier to undersand user friendly message. Instead of returning the raw error. And log the raw error.

errors_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ func TestError_Type(t *testing.T) {
142142
}
143143
}
144144

145-
// ignoreTestSetDefaultType if run, will fail other tests because New's type would be reset
146-
func ignoreTestSetDefaultType(t *testing.T) {
145+
func TestSetDefaultType(t *testing.T) {
147146
type args struct {
148147
message string
149148
e errType

helper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ func NewWithErrMsgType(e error, message string, etype errType) *Error {
2828
return newerr(e, message, file, line, etype)
2929
}
3030

31+
// Internal helper method for creation internal errors
32+
func Internal(message string) *Error {
33+
_, file, line, _ := runtime.Caller(1)
34+
return newerr(nil, message, file, line, TypeInternal)
35+
}
36+
3137
// Validation is a helper function to create a new error of type TypeValidation
3238
func Validation(message string) *Error {
3339
_, file, line, _ := runtime.Caller(1)

helper_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestHelperFnsForAllTypes(t *testing.T) {
101101
switch tt.args.eType {
102102
case TypeInternal:
103103
{
104-
got = New(tt.args.message)
104+
got = Internal(tt.args.message)
105105
_, file, line, _ := runtime.Caller(0)
106106
line--
107107
want = newerr(nil, tt.args.message, file, line, TypeInternal)
@@ -482,7 +482,7 @@ func TestHTTPStatusCodeMessage(t *testing.T) {
482482
{
483483
name: "TypeInternal",
484484
args: args{
485-
err: New("unknown error occurred"),
485+
err: Internal("unknown error occurred"),
486486
},
487487
want: http.StatusInternalServerError,
488488
want1: "unknown error occurred",
@@ -581,7 +581,7 @@ func TestHTTPStatusCodeMessage(t *testing.T) {
581581
{
582582
name: "TypeDownstreamDependencyTimedout",
583583
args: args{
584-
err: New("dependency timed out"),
584+
err: DownstreamDependencyTimedout("dependency timed out"),
585585
},
586586
want: http.StatusInternalServerError,
587587
want1: "dependency timed out",
@@ -618,7 +618,7 @@ func TestWriteHTTP(t *testing.T) {
618618
{
619619
name: "TypeInternal",
620620
args: args{
621-
err: New("system error"),
621+
err: Internal("system error"),
622622
w: httptest.NewRecorder(),
623623
},
624624
wantStatus: http.StatusInternalServerError,

0 commit comments

Comments
 (0)