github.com/blend/go-sdk@v1.20220411.3/envoyutil/xfcc_errors_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package envoyutil_test
     9  
    10  import (
    11  	"encoding/json"
    12  	"testing"
    13  
    14  	sdkAssert "github.com/blend/go-sdk/assert"
    15  	"github.com/blend/go-sdk/ex"
    16  
    17  	"github.com/blend/go-sdk/envoyutil"
    18  )
    19  
    20  func TestXFCCExtractionErrorMarshal(t *testing.T) {
    21  	assert := sdkAssert.New(t)
    22  
    23  	c := ex.Class("caused by bad extraction")
    24  	err := &envoyutil.XFCCExtractionError{Class: c, XFCC: "a=b", Metadata: map[string]string{"x": "why"}}
    25  
    26  	asBytes, marshalErr := json.MarshalIndent(err, "", "  ")
    27  	assert.Nil(marshalErr)
    28  	expected := `{
    29    "class": "caused by bad extraction",
    30    "xfcc": "a=b",
    31    "metadata": {
    32      "x": "why"
    33    }
    34  }`
    35  	assert.Equal(expected, string(asBytes))
    36  }
    37  
    38  func TestXFCCExtractionErrorError(t *testing.T) {
    39  	assert := sdkAssert.New(t)
    40  
    41  	c := ex.Class("oh a bad thing happened")
    42  	var err error = &envoyutil.XFCCExtractionError{Class: c}
    43  	assert.Equal(c, err.Error())
    44  }
    45  
    46  func TestIsExtractionError(t *testing.T) {
    47  	assert := sdkAssert.New(t)
    48  
    49  	var err error = ex.New("NOPE")
    50  	assert.False(envoyutil.IsExtractionError(err))
    51  	err = &envoyutil.XFCCExtractionError{Class: "YEP"}
    52  	assert.True(envoyutil.IsExtractionError(err))
    53  }
    54  
    55  func TestXFCCValidationErrorMarshal(t *testing.T) {
    56  	assert := sdkAssert.New(t)
    57  
    58  	c := ex.Class("caused by something invalid")
    59  	err := &envoyutil.XFCCValidationError{Class: c, XFCC: "mm=hm", Metadata: map[string]string{"ecks": "y"}}
    60  
    61  	asBytes, marshalErr := json.MarshalIndent(err, "", "  ")
    62  	assert.Nil(marshalErr)
    63  	expected := `{
    64    "class": "caused by something invalid",
    65    "xfcc": "mm=hm",
    66    "metadata": {
    67      "ecks": "y"
    68    }
    69  }`
    70  	assert.Equal(expected, string(asBytes))
    71  }
    72  
    73  func TestXFCCValidationErrorError(t *testing.T) {
    74  	assert := sdkAssert.New(t)
    75  
    76  	c := ex.Class("oh an invalid thing happened")
    77  	var err error = &envoyutil.XFCCValidationError{Class: c}
    78  	assert.Equal(c, err.Error())
    79  }
    80  
    81  func TestIsValidationError(t *testing.T) {
    82  	assert := sdkAssert.New(t)
    83  
    84  	var err error = ex.New("NOPE")
    85  	assert.False(envoyutil.IsValidationError(err))
    86  	err = &envoyutil.XFCCValidationError{Class: "YEP"}
    87  	assert.True(envoyutil.IsValidationError(err))
    88  }
    89  
    90  func TestXFCCFatalErrorMarshal(t *testing.T) {
    91  	assert := sdkAssert.New(t)
    92  
    93  	c := ex.Class("caused by something fatal")
    94  	err := &envoyutil.XFCCFatalError{Class: c, XFCC: "c=d"}
    95  
    96  	asBytes, marshalErr := json.MarshalIndent(err, "", "  ")
    97  	assert.Nil(marshalErr)
    98  	expected := `{
    99    "class": "caused by something fatal",
   100    "xfcc": "c=d"
   101  }`
   102  	assert.Equal(expected, string(asBytes))
   103  }
   104  
   105  func TestXFCCFatalErrorError(t *testing.T) {
   106  	assert := sdkAssert.New(t)
   107  
   108  	c := ex.Class("oh a fatal thing happened")
   109  	var err error = &envoyutil.XFCCFatalError{Class: c}
   110  	assert.Equal(c, err.Error())
   111  }
   112  
   113  func TestIsFatalError(t *testing.T) {
   114  	assert := sdkAssert.New(t)
   115  
   116  	var err error = ex.New("NOPE")
   117  	assert.False(envoyutil.IsFatalError(err))
   118  	err = &envoyutil.XFCCFatalError{Class: "YEP"}
   119  	assert.True(envoyutil.IsFatalError(err))
   120  }