github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/errors/apierror_test.go (about)

     1  package errors
     2  
     3  import (
     4  	stderrors "errors"
     5  	"testing"
     6  
     7  	"github.com/safedep/dry/api"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestAsApiError(t *testing.T) {
    12  	cases := []struct {
    13  		name string
    14  		err  error
    15  		isOk bool
    16  	}{
    17  		{
    18  			"Unwrappable error",
    19  			stderrors.New("err"),
    20  			false,
    21  		},
    22  		{
    23  			"Wrappable error",
    24  			BuildApiError(api.ApiErrorTypeInvalidRequest,
    25  				api.ApiErrorCodeAppGenericError, "Test"),
    26  			true,
    27  		},
    28  	}
    29  
    30  	for _, test := range cases {
    31  		t.Run(test.name, func(t *testing.T) {
    32  			err, ok := AsApiError(test.err)
    33  			assert.NotNil(t, err)
    34  			assert.Equal(t, test.isOk, ok)
    35  		})
    36  	}
    37  }
    38  
    39  func TestApiErrorBuilder(t *testing.T) {
    40  	err := BuildApiError("test_type", "200", "Test error")
    41  	assert.NotNil(t, err)
    42  
    43  	assert.Equal(t, api.ApiErrorCode("200"), *err.ApiError().Code)
    44  	assert.Equal(t, api.ApiErrorType("test_type"), *err.ApiError().Type)
    45  	assert.Equal(t, "Test error", *err.ApiError().Message)
    46  }
    47  
    48  func TestApiErrorAddParams(t *testing.T) {
    49  	err := BuildApiError("test_type", "200", "Test error")
    50  	err.AddParam("p1", "v1")
    51  
    52  	assert.NotNil(t, err.apiErr.Params)
    53  
    54  	v, ok := err.apiErr.Params.Get("p1")
    55  	assert.True(t, ok)
    56  	assert.Equal(t, "v1", *v.Value)
    57  
    58  	_, ok = err.apiErr.Params.Get("p2")
    59  	assert.False(t, ok)
    60  }
    61  
    62  func TestUnmarshalApiError(t *testing.T) {
    63  	cases := []struct {
    64  		name    string
    65  		body    string
    66  		ok      bool
    67  		errType string
    68  		code    string
    69  	}{
    70  		{
    71  			"Valid error JSON",
    72  			`
    73  			{
    74  				"code": "api_guard_invalid_credentials",
    75  				"message": "Key has expired, please renew",
    76  				"type": "invalid_request"
    77  			}
    78  			`,
    79  			true,
    80  			"invalid_request",
    81  			"api_guard_invalid_credentials",
    82  		},
    83  		{
    84  			"Valid JSON but schema mismatch",
    85  			`
    86  			{
    87  				"a": "b"
    88  			}
    89  			`,
    90  			false,
    91  			string(api.ApiErrorTypeInternalError),
    92  			string(api.ApiErrorCodeAppGenericError),
    93  		},
    94  		{
    95  			"Invalid JSON",
    96  			`
    97  			NOT A JSON
    98  			`,
    99  			false,
   100  			string(api.ApiErrorTypeInternalError),
   101  			string(api.ApiErrorCodeAppGenericError),
   102  		},
   103  	}
   104  
   105  	for _, test := range cases {
   106  		apiErr, ok := UnmarshalApiError([]byte(test.body))
   107  		if test.ok {
   108  			assert.True(t, ok)
   109  			assert.Equal(t, test.errType, string(*apiErr.apiErr.Type))
   110  			assert.Equal(t, test.code, string(*apiErr.apiErr.Code))
   111  		} else {
   112  			assert.False(t, ok)
   113  			assert.Equal(t, string(api.ApiErrorTypeInternalError),
   114  				string(*apiErr.apiErr.Type))
   115  		}
   116  	}
   117  }
   118  
   119  func TestApiErrorErrorMessage(t *testing.T) {
   120  	errJson := `
   121  			{
   122  				"code": "api_guard_invalid_credentials",
   123  				"message": "Key has expired, please renew",
   124  				"type": "invalid_request",
   125  				"params": {
   126  					"key1": {
   127  						"key": "key1",
   128  						"value": "value1"
   129  					}
   130  				}
   131  			}
   132  			`
   133  	apiErr, ok := UnmarshalApiError([]byte(errJson))
   134  	assert.True(t, ok)
   135  	assert.ErrorContains(t, apiErr, "Code=api_guard_invalid_credentials")
   136  	assert.ErrorContains(t, apiErr, "Type=invalid_request")
   137  	assert.ErrorContains(t, apiErr, "Params=[ key1:\"value1\" ]")
   138  }
   139  
   140  func TestApiErrorErrorMessageWithoutParams(t *testing.T) {
   141  	errJson := `
   142  			{
   143  				"code": "api_guard_invalid_credentials",
   144  				"message": "Key has expired, please renew",
   145  				"type": "invalid_request"
   146  			}
   147  			`
   148  	apiErr, ok := UnmarshalApiError([]byte(errJson))
   149  	assert.True(t, ok)
   150  	assert.ErrorContains(t, apiErr, "Code=api_guard_invalid_credentials")
   151  	assert.ErrorContains(t, apiErr, "Type=invalid_request")
   152  	assert.ErrorContains(t, apiErr, "Params=[]")
   153  
   154  }