Skip to content

Commit

Permalink
optimize error process logic for mir
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Nov 15, 2022
1 parent 7e64ff3 commit a33cb7d
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 149 deletions.
12 changes: 6 additions & 6 deletions core/descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func TestDescriptors(t *testing.T) {
Comment: "",
Fields: []*FieldDescriptor{
{
Host: "",
Path: "/",
Queries: nil,
HttpMethod: mir.MethodGet,
MethodName: "Index",
Comment: "",
Host: "",
Path: "/",
Queries: nil,
HttpMethods: []string{mir.MethodGet},
MethodName: "Index",
Comment: "",
},
},
}); err != nil {
Expand Down
46 changes: 46 additions & 0 deletions error.go
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
}
93 changes: 93 additions & 0 deletions error_test.go
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)
}
}
}
53 changes: 27 additions & 26 deletions examples/mirc/auto/api/site.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a33cb7d

Please sign in to comment.