Skip to content

Commit 7fee94f

Browse files
committed
[patch] refactor for better readability (comments, some functions and README)
1 parent 10e1d01 commit 7fee94f

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Errors package is a drop-in replacement of the built-in Go errors package with n
1818
5. Helper functions to generate each error type
1919
6. Helper function to get error Type, error type as int, check if error type is wrapped anywhere in chain
2020

21-
In case of nested errors, the messages (in case of nesting with this package's error) & errors are also looped through the full chain of errors.
21+
In case of nested errors, the messages & errors are also looped through the full chain of errors.
2222

2323
### Prerequisites
2424

@@ -88,7 +88,7 @@ If error is nested with multiple errors, it loops through all the levels and ret
8888

8989
Before that, over the years I have tried error with stack trace, annotation, custom error package with error codes etc. Finally, I think this package gives the best of all worlds, for most generic usecases.
9090

91-
A sample was already shown in the user friendly message section, following one would show 1-2 scenarios.
91+
A sample was already shown in the user friendly message section, following one would show a few more scenarios.
9292

9393
```golang
9494
package main
@@ -165,16 +165,16 @@ func main() {
165165

166166
```
167167

168-
[webgo](https://github.com/bnkamalesh/webgo) was used to illustrate the usage of the function, `errors.HTTPStatusCodeMessage`. It returns the appropriate http status code, user friendly message stored within, and a boolean value. Boolean value is `true` if the returned error is of this package's error type.
168+
[webgo](https://github.com/bnkamalesh/webgo) was used to illustrate the usage of the function, `errors.HTTPStatusCodeMessage`. It returns the appropriate http status code, user friendly message stored within, and a boolean value. Boolean value is `true` if the returned error of type *Error.
169169
Since we get the status code and message separately, when using any web framework, you can set values according to the respective framework's native functions. In case of Webgo, it wraps errors in a struct of its own. Otherwise, you could directly respond to the HTTP request by calling `errors.WriteHTTP(error,http.ResponseWriter)`.
170170

171171
Once the app is running, you can check the response by opening `http://localhost:8080` on your browser. Or on terminal
172172
```bash
173173
$ curl http://localhost:8080
174-
{"errors":"we lost bar2!. bar2 was deceived by bar1 :(","status":500} // HTTP response
174+
{"errors":"we lost bar2!. bar2 was deceived by bar1 :(","status":500} // output
175175
```
176176

177-
And output of the `fmt.Println(err.Error())`
177+
And the `fmt.Println(err.Error())` generated output on stdout would be:
178178
```bash
179179
/Users/username/go/src/errorscheck/main.go:28 /Users/username/go/src/errorscheck/main.go:20 sinking bar
180180
```

errors.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ func (e *Error) Unwrap() error {
116116
// Is implements the Is interface required by Go
117117
func (e *Error) Is(err error) bool {
118118
o, _ := err.(*Error)
119-
if o == nil {
120-
return false
121-
}
122-
return o == e
119+
return o != nil && o == e
123120
}
124121

125122
// HTTPStatusCode is a convenience method used to get the appropriate HTTP response status code for the respective error type

helper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func newerr(e error, message string, file string, line int, etype errType) *Erro
1212
original: e,
1313
message: message,
1414
eType: etype,
15-
// '+' concatenation is much faster than using fmt.Sprintf("%s:%d", file, line)
15+
// '+' concatenation is ~100x faster than using fmt.Sprintf("%s:%d", file, line)
1616
fileLine: file + ":" + strconv.Itoa(line),
1717
}
1818
}
@@ -29,7 +29,7 @@ func Wrap(err error) *Error {
2929
return newerr(err, "", file, line, e.eType)
3030
}
3131

32-
// WrapWithMsg wrap error with a message
32+
// WrapWithMsg wrap error with a user friendly message
3333
func WrapWithMsg(err error, msg string) *Error {
3434
_, file, line, _ := runtime.Caller(1)
3535
e, _ := err.(*Error)
@@ -51,7 +51,7 @@ func NewWithErrMsgType(e error, message string, etype errType) *Error {
5151
return newerr(e, message, file, line, etype)
5252
}
5353

54-
// Internal helper method for creation internal errors
54+
// Internal helper method for creating internal errors
5555
func Internal(message string) *Error {
5656
_, file, line, _ := runtime.Caller(1)
5757
return newerr(nil, message, file, line, TypeInternal)

helper_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ func TestWriteHTTP(t *testing.T) {
645645
}
646646
}
647647

648-
func TestHasCheck(t *testing.T) {
648+
func TestHasType(t *testing.T) {
649649
type args struct {
650650
err error
651651
et errType

0 commit comments

Comments
 (0)