From dfd85cd871368822196cc3323ad66c99f473e72d Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 20 Jan 2022 00:28:09 +0300 Subject: [PATCH 1/3] logger: add logger Fields test Signed-off-by: Vasiliy Tolstov --- logger/logger_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/logger/logger_test.go b/logger/logger_test.go index ef23f6eb..82cadb3a 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -7,6 +7,19 @@ import ( "testing" ) +func TestFields(t *testing.T) { + ctx := context.TODO() + buf := bytes.NewBuffer(nil) + l := NewLogger(WithLevel(TraceLevel), WithOutput(buf)) + if err := l.Init(); err != nil { + t.Fatal(err) + } + l.Fields("key", "val").Info(ctx, "message") + if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) { + t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes()) + } +} + func TestClone(t *testing.T) { ctx := context.TODO() buf := bytes.NewBuffer(nil) From d8fe2ff8b4baba36d8d9447e1dbe83ecaf19aaed Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 20 Jan 2022 15:29:21 +0300 Subject: [PATCH 2/3] add logger context test Signed-off-by: Vasiliy Tolstov --- logger/logger_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/logger/logger_test.go b/logger/logger_test.go index 82cadb3a..dd61487e 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -7,6 +7,24 @@ import ( "testing" ) +func TestContext(t *testing.T) { + ctx := context.TODO() + buf := bytes.NewBuffer(nil) + l := NewLogger(WithLevel(TraceLevel), WithOutput(buf)) + if err := l.Init(); err != nil { + t.Fatal(err) + } + + nl, ok := FromContext(NewContext(ctx, l.Fields("key", "val"))) + if !ok { + t.Fatal("context without logger") + } + nl.Info(ctx, "message") + if !bytes.Contains(buf.Bytes(), []byte(`"key":"val"`)) { + t.Fatalf("logger fields not works, buf contains: %s", buf.Bytes()) + } +} + func TestFields(t *testing.T) { ctx := context.TODO() buf := bytes.NewBuffer(nil) From 534bce2d2028a01cbe493025380e776409d1d490 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 21 Jan 2022 00:50:16 +0300 Subject: [PATCH 3/3] 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; +}