-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize error process logic for mir
- Loading branch information
Showing
13 changed files
with
336 additions
and
149 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,46 @@ | ||
// Copyright 2022 Michael Li <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by Apache License 2.0 that | ||
// can be found in the LICENSE file. | ||
|
||
package mir | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Error indicator error's wraper | ||
type Error interface { | ||
error | ||
StatusCode() int | ||
} | ||
|
||
type httpError struct { | ||
error | ||
statusCode int | ||
} | ||
|
||
func NewError(statusCode int, err error) Error { | ||
return &httpError{ | ||
error: err, | ||
statusCode: statusCode, | ||
} | ||
} | ||
|
||
func Errorf(statusCode int, format string, a ...any) Error { | ||
return &httpError{ | ||
error: fmt.Errorf(format, a...), | ||
statusCode: statusCode, | ||
} | ||
} | ||
|
||
func Errorln(statusCode int, text string) Error { | ||
return &httpError{ | ||
error: errors.New(text), | ||
statusCode: statusCode, | ||
} | ||
} | ||
|
||
func (e *httpError) StatusCode() int { | ||
return e.statusCode | ||
} |
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,93 @@ | ||
package mir | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestNewError(t *testing.T) { | ||
for _, data := range []struct { | ||
code int | ||
err error | ||
msg string | ||
}{ | ||
{ | ||
code: http.StatusInternalServerError, | ||
err: errors.New(http.StatusText(http.StatusInternalServerError)), | ||
msg: http.StatusText(http.StatusInternalServerError), | ||
}, | ||
{ | ||
code: http.StatusMethodNotAllowed, | ||
err: errors.New(http.StatusText(http.StatusMethodNotAllowed)), | ||
msg: http.StatusText(http.StatusMethodNotAllowed), | ||
}, | ||
} { | ||
err := NewError(data.code, data.err) | ||
code, msg := err.StatusCode(), err.Error() | ||
if code != data.code { | ||
t.Errorf("expect error code: %d but got: %d", data.code, code) | ||
} | ||
if msg != data.msg { | ||
t.Errorf("expect error msg: %s but got: %s", data.msg, msg) | ||
} | ||
} | ||
} | ||
|
||
func TestErrorf(t *testing.T) { | ||
for _, data := range []struct { | ||
code int | ||
format string | ||
a []any | ||
}{ | ||
{ | ||
code: http.StatusInternalServerError, | ||
format: `internal server error: %s:%s`, | ||
a: []any{"host", "localhost"}, | ||
}, | ||
{ | ||
code: http.StatusMethodNotAllowed, | ||
format: `method not allowed: %s:%s`, | ||
a: []any{"method", "POST"}, | ||
}, | ||
} { | ||
err := Errorf(data.code, data.format, data.a...) | ||
fmtErrMsg := fmt.Errorf(data.format, data.a...).Error() | ||
code, msg := err.StatusCode(), err.Error() | ||
if code != data.code { | ||
t.Errorf("expect error code: %d but got: %d", data.code, code) | ||
} | ||
if msg != fmtErrMsg { | ||
t.Errorf("expect error msg: %s but got: %s", fmtErrMsg, msg) | ||
} | ||
} | ||
} | ||
|
||
func TestErrorln(t *testing.T) { | ||
for _, data := range []struct { | ||
code int | ||
text string | ||
msg string | ||
}{ | ||
{ | ||
code: http.StatusInternalServerError, | ||
text: http.StatusText(http.StatusInternalServerError), | ||
msg: http.StatusText(http.StatusInternalServerError), | ||
}, | ||
{ | ||
code: http.StatusMethodNotAllowed, | ||
text: http.StatusText(http.StatusMethodNotAllowed), | ||
msg: http.StatusText(http.StatusMethodNotAllowed), | ||
}, | ||
} { | ||
err := Errorln(data.code, data.text) | ||
code, msg := err.StatusCode(), err.Error() | ||
if code != data.code { | ||
t.Errorf("expect error code: %d but got: %d", data.code, code) | ||
} | ||
if msg != data.msg { | ||
t.Errorf("expect error msg: %s but got: %s", data.msg, msg) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.