From 534bce2d2028a01cbe493025380e776409d1d490 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 21 Jan 2022 00:50:16 +0300 Subject: [PATCH] errors: add proto Signed-off-by: Vasiliy Tolstov --- errors/errors.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ errors/errors.proto | 31 ++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 errors/errors.proto diff --git a/errors/errors.go b/errors/errors.go index afad0c64..12daa47d 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -6,6 +6,8 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" + "strings" ) var ( @@ -238,3 +240,64 @@ func FromError(err error) *Error { return Parse(err.Error()) } + +// MarshalJSON returns error data +func (e *Error) MarshalJSON() ([]byte, error) { + return e.Marshal() +} + +// UnmarshalJSON set error data +func (e *Error) UnmarshalJSON(data []byte) error { + return e.Unmarshal(data) +} + +// ProtoMessage noop func +func (e *Error) ProtoMessage() {} + +// Reset resets error +func (e *Error) Reset() { + *e = Error{} +} + +// String returns error as string +func (e *Error) String() string { + return fmt.Sprintf(`{"id":"%s","detail":"%s","status":"%s","code":%d}`, e.ID, e.Detail, e.Status, e.Code) +} + +// Marshal returns error data +func (e *Error) Marshal() ([]byte, error) { + return []byte(e.String()), nil +} + +// Unmarshal set error data +func (e *Error) Unmarshal(data []byte) error { + str := string(data) + if len(data) < 41 { + return fmt.Errorf("invalid data") + } + parts := strings.FieldsFunc(str[1:len(str)-1], func(r rune) bool { + return r == ',' + }) + for _, part := range parts { + nparts := strings.FieldsFunc(part, func(r rune) bool { + return r == ':' + }) + for idx := 0; idx < len(nparts); idx++ { + switch { + case nparts[idx] == `"id"`: + e.ID = nparts[idx+1][1 : len(nparts[idx+1])-1] + case nparts[idx] == `"detail"`: + e.Detail = nparts[idx+1][1 : len(nparts[idx+1])-1] + case nparts[idx] == `"status"`: + e.Status = nparts[idx+1][1 : len(nparts[idx+1])-1] + case nparts[idx] == `"code"`: + c, err := strconv.ParseInt(nparts[idx+1], 10, 32) + if err != nil { + return err + } + e.Code = int32(c) + } + } + } + return nil +} diff --git a/errors/errors.proto b/errors/errors.proto new file mode 100644 index 00000000..76164b86 --- /dev/null +++ b/errors/errors.proto @@ -0,0 +1,31 @@ +// Copyright 2021 Unistack LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package micro.errors; + +option cc_enable_arenas = true; +option go_package = "go.unistack.org/micro/v3/errors;errors"; +option java_multiple_files = true; +option java_outer_classname = "MicroErrors"; +option java_package = "micro.errors"; +option objc_class_prefix = "MERRORS"; + +message Error { + string id = 1; + string detail = 2; + string status = 3; + uint32 code = 4; +}