github.com/cloudwego/hertz@v0.9.3/pkg/common/errors/errors_test.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * The MIT License (MIT)
    16   *
    17   * Copyright (c) 2014 Manuel Martínez-Almeida
    18   *
    19   * Permission is hereby granted, free of charge, to any person obtaining a copy
    20   * of this software and associated documentation files (the "Software"), to deal
    21   * in the Software without restriction, including without limitation the rights
    22   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    23   * copies of the Software, and to permit persons to whom the Software is
    24   * furnished to do so, subject to the following conditions:
    25   *
    26   * The above copyright notice and this permission notice shall be included in
    27   * all copies or substantial portions of the Software.
    28   *
    29   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    30   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    31   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    32   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    33   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    34   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    35   * THE SOFTWARE.
    36   *
    37   * This file may have been modified by CloudWeGo authors. All CloudWeGo
    38   * Modifications are Copyright 2022 CloudWeGo Authors
    39   */
    40  
    41  package errors
    42  
    43  import (
    44  	"errors"
    45  	"testing"
    46  
    47  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    48  )
    49  
    50  func TestError(t *testing.T) {
    51  	baseError := errors.New("test error")
    52  	err := &Error{
    53  		Err:  baseError,
    54  		Type: ErrorTypePrivate,
    55  	}
    56  	assert.DeepEqual(t, err.Error(), baseError.Error())
    57  	assert.DeepEqual(t, map[string]interface{}{"error": baseError.Error()}, err.JSON())
    58  
    59  	assert.DeepEqual(t, err.SetType(ErrorTypePublic), err)
    60  	assert.DeepEqual(t, ErrorTypePublic, err.Type)
    61  
    62  	assert.DeepEqual(t, err.SetMeta("some data"), err)
    63  	assert.DeepEqual(t, "some data", err.Meta)
    64  	assert.DeepEqual(t, map[string]interface{}{
    65  		"error": baseError.Error(),
    66  		"meta":  "some data",
    67  	}, err.JSON())
    68  
    69  	err.SetMeta(map[string]interface{}{ // nolint: errcheck
    70  		"status": "200",
    71  		"data":   "some data",
    72  	})
    73  	assert.DeepEqual(t, map[string]interface{}{
    74  		"error":  baseError.Error(),
    75  		"status": "200",
    76  		"data":   "some data",
    77  	}, err.JSON())
    78  
    79  	err.SetMeta(map[string]interface{}{ // nolint: errcheck
    80  		"error":  "custom error",
    81  		"status": "200",
    82  		"data":   "some data",
    83  	})
    84  	assert.DeepEqual(t, map[string]interface{}{
    85  		"error":  "custom error",
    86  		"status": "200",
    87  		"data":   "some data",
    88  	}, err.JSON())
    89  
    90  	type customError struct {
    91  		status string
    92  		data   string
    93  	}
    94  	err.SetMeta(customError{status: "200", data: "other data"}) // nolint: errcheck
    95  	assert.DeepEqual(t, customError{status: "200", data: "other data"}, err.JSON())
    96  }
    97  
    98  func TestErrorSlice(t *testing.T) {
    99  	errs := ErrorChain{
   100  		{Err: errors.New("first"), Type: ErrorTypePrivate},
   101  		{Err: errors.New("second"), Type: ErrorTypePrivate, Meta: "some data"},
   102  		{Err: errors.New("third"), Type: ErrorTypePublic, Meta: map[string]interface{}{"status": "400"}},
   103  	}
   104  
   105  	assert.DeepEqual(t, errs, errs.ByType(ErrorTypeAny))
   106  	assert.DeepEqual(t, "third", errs.Last().Error())
   107  	assert.DeepEqual(t, []string{"first", "second", "third"}, errs.Errors())
   108  	assert.DeepEqual(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())
   109  	assert.DeepEqual(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())
   110  	assert.DeepEqual(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())
   111  	assert.DeepEqual(t, "", errs.ByType(ErrorTypeBind).String())
   112  	assert.DeepEqual(t, `Error #01: first
   113  Error #02: second
   114       Meta: some data
   115  Error #03: third
   116       Meta: map[status:400]
   117  `, errs.String())
   118  	assert.DeepEqual(t, []interface{}{
   119  		map[string]interface{}{"error": "first"},
   120  		map[string]interface{}{"error": "second", "meta": "some data"},
   121  		map[string]interface{}{"error": "third", "status": "400"},
   122  	}, errs.JSON())
   123  	errs = ErrorChain{
   124  		{Err: errors.New("first"), Type: ErrorTypePrivate},
   125  	}
   126  
   127  	assert.DeepEqual(t, map[string]interface{}{"error": "first"}, errs.JSON())
   128  	errs = ErrorChain{}
   129  	assert.DeepEqual(t, true, errs.Last() == nil)
   130  	assert.Nil(t, errs.JSON())
   131  	assert.DeepEqual(t, "", errs.String())
   132  }
   133  
   134  func TestErrorFormat(t *testing.T) {
   135  	err := Newf(ErrorTypeAny, nil, "caused by %s", "reason")
   136  	assert.DeepEqual(t, New(errors.New("caused by reason"), ErrorTypeAny, nil), err)
   137  	publicErr := NewPublicf("caused by %s", "reason")
   138  	assert.DeepEqual(t, New(errors.New("caused by reason"), ErrorTypePublic, nil), publicErr)
   139  	privateErr := NewPrivatef("caused by %s", "reason")
   140  	assert.DeepEqual(t, New(errors.New("caused by reason"), ErrorTypePrivate, nil), privateErr)
   141  }