github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/errors/errors_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/errors/errors_test.go 14 package errors 15 16 import ( 17 er "errors" 18 "net/http" 19 "testing" 20 ) 21 22 func TestFromError(t *testing.T) { 23 err := NotFound("go.micro.test", "%s", "example") 24 merr := FromError(err) 25 if merr.Id != "go.micro.test" || merr.Code != 404 { 26 t.Fatalf("invalid conversation %v != %v", err, merr) 27 } 28 err = er.New(err.Error()) 29 merr = FromError(err) 30 if merr.Id != "go.micro.test" || merr.Code != 404 { 31 t.Fatalf("invalid conversation %v != %v", err, merr) 32 } 33 34 } 35 36 func TestEqual(t *testing.T) { 37 err1 := NotFound("myid1", "msg1") 38 err2 := NotFound("myid2", "msg2") 39 40 if !Equal(err1, err2) { 41 t.Fatal("errors must be equal") 42 } 43 44 err3 := er.New("my test err") 45 if Equal(err1, err3) { 46 t.Fatal("errors must be not equal") 47 } 48 49 } 50 51 func TestErrors(t *testing.T) { 52 testData := []*Error{ 53 { 54 Id: "test", 55 Code: 500, 56 Detail: "Internal server error", 57 Status: http.StatusText(500), 58 }, 59 } 60 61 for _, e := range testData { 62 ne := New(e.Id, e.Detail, e.Code) 63 64 if e.Error() != ne.Error() { 65 t.Fatalf("Expected %s got %s", e.Error(), ne.Error()) 66 } 67 68 pe := Parse(ne.Error()) 69 70 if pe == nil { 71 t.Fatalf("Expected error got nil %v", pe) 72 } 73 74 if pe.Id != e.Id { 75 t.Fatalf("Expected %s got %s", e.Id, pe.Id) 76 } 77 78 if pe.Detail != e.Detail { 79 t.Fatalf("Expected %s got %s", e.Detail, pe.Detail) 80 } 81 82 if pe.Code != e.Code { 83 t.Fatalf("Expected %d got %d", e.Code, pe.Code) 84 } 85 86 if pe.Status != e.Status { 87 t.Fatalf("Expected %s got %s", e.Status, pe.Status) 88 } 89 } 90 }