From 09973af099cdf44c2d68a314ee968a06be0b802f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 22 Feb 2021 00:52:18 +0300 Subject: [PATCH] server: add error helper Signed-off-by: Vasiliy Tolstov --- server/errors.go | 59 +++++++++++++++++++++++++++++++++++++++++++ server/errors_test.go | 19 ++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 server/errors.go create mode 100644 server/errors_test.go diff --git a/server/errors.go b/server/errors.go new file mode 100644 index 00000000..b9dc4dac --- /dev/null +++ b/server/errors.go @@ -0,0 +1,59 @@ +package server + +import "github.com/unistack-org/micro/v3/errors" + +type Error struct { + id string +} + +func NewError(id string) *Error { + return &Error{id} +} + +func (e *Error) BadRequest(format string, a ...interface{}) error { + return errors.BadRequest(e.id, format, a...) +} + +func (e *Error) Unauthorized(format string, a ...interface{}) error { + return errors.Unauthorized(e.id, format, a...) +} + +func (e *Error) Forbidden(format string, a ...interface{}) error { + return errors.Forbidden(e.id, format, a...) +} + +func (e *Error) NotFound(format string, a ...interface{}) error { + return errors.NotFound(e.id, format, a...) +} + +func (e *Error) MethodNotAllowed(format string, a ...interface{}) error { + return errors.MethodNotAllowed(e.id, format, a...) +} + +func (e *Error) Timeout(format string, a ...interface{}) error { + return errors.Timeout(e.id, format, a...) +} + +func (e *Error) Conflict(format string, a ...interface{}) error { + return errors.Conflict(e.id, format, a...) +} + +func (e *Error) InternalServerError(format string, a ...interface{}) error { + return errors.InternalServerError(e.id, format, a...) +} + +func (e *Error) NotImplemented(format string, a ...interface{}) error { + return errors.NotImplemented(e.id, format, a...) +} + +func (e *Error) BadGateway(format string, a ...interface{}) error { + return errors.BadGateway(e.id, format, a...) +} + +func (e *Error) ServiceUnavailable(format string, a ...interface{}) error { + return errors.ServiceUnavailable(e.id, format, a...) +} + +func (e *Error) GatewayTimeout(format string, a ...interface{}) error { + return errors.GatewayTimeout(e.id, format, a...) +} diff --git a/server/errors_test.go b/server/errors_test.go new file mode 100644 index 00000000..ffa3aa90 --- /dev/null +++ b/server/errors_test.go @@ -0,0 +1,19 @@ +package server + +import ( + "testing" + + "github.com/unistack-org/micro/v3/errors" +) + +func TestError(t *testing.T) { + e := NewError("svc1") + err := e.BadRequest("%s", "test") + merr, ok := err.(*errors.Error) + if !ok { + t.Fatal("error not *errors.Error") + } + if merr.Id != "svc1" { + t.Fatal("id != svc1") + } +}