|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + stderrors "errors" |
| 5 | + "math" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + meshkiterrors "github.com/meshery/meshkit/errors" |
| 10 | +) |
| 11 | + |
| 12 | +func TestErrorConstructorsExposeExpectedCodes(t *testing.T) { |
| 13 | + testErr := stderrors.New("boom") |
| 14 | + |
| 15 | + testCases := []struct { |
| 16 | + name string |
| 17 | + err error |
| 18 | + code string |
| 19 | + }{ |
| 20 | + {"invalid construct schema version", ErrInvalidConstructSchemaVersion("design", "v0", "v1"), ErrInvalidSchemaVersionCode}, |
| 21 | + {"cue lookup", ErrCueLookup(testErr), ErrCueLookupCode}, |
| 22 | + {"json schema to cue", ErrJsonSchemaToCue(testErr), ErrJsonSchemaToCueCode}, |
| 23 | + {"yaml to cue", ErrYamlToCue(testErr), ErrYamlToCueCode}, |
| 24 | + {"json to cue", ErrJsonToCue(testErr), ErrJsonToCueCode}, |
| 25 | + {"expected type mismatch", ErrExpectedTypeMismatch(testErr, "string"), ErrExpectedTypeMismatchCode}, |
| 26 | + {"missing field", ErrMissingField(testErr, "name"), ErrMissingFieldCode}, |
| 27 | + {"unmarshal", ErrUnmarshal(testErr), ErrUnmarshalCode}, |
| 28 | + {"unmarshal invalid", ErrUnmarshalInvalid(testErr, reflect.TypeOf("")), ErrUnmarshalInvalidCode}, |
| 29 | + {"unmarshal syntax", ErrUnmarshalSyntax(testErr, 42), ErrUnmarshalSyntaxCode}, |
| 30 | + {"unmarshal type", ErrUnmarshalType(testErr, "field"), ErrUnmarshalTypeCode}, |
| 31 | + {"unmarshal unsupported type", ErrUnmarshalUnsupportedType(testErr, reflect.TypeOf(func() {})), ErrUnmarshalUnsupportedTypeCode}, |
| 32 | + {"unmarshal unsupported value", ErrUnmarshalUnsupportedValue(testErr, reflect.ValueOf(math.Inf(1))), ErrUnmarshalUnsupportedValueCode}, |
| 33 | + {"marshal", ErrMarshal(testErr), ErrMarshalCode}, |
| 34 | + {"get bool", ErrGetBool("enabled", testErr), ErrGetBoolCode}, |
| 35 | + {"remote file not found", ErrRemoteFileNotFound("https://example.com/file"), ErrRemoteFileNotFoundCode}, |
| 36 | + {"reading remote file", ErrReadingRemoteFile(testErr), ErrReadingRemoteFileCode}, |
| 37 | + {"reading local file", ErrReadingLocalFile(testErr), ErrReadingLocalFileCode}, |
| 38 | + {"read file", ErrReadFile(testErr, "/tmp/file"), ErrReadFileCode}, |
| 39 | + {"write file", ErrWriteFile(testErr, "/tmp/file"), ErrWriteFileCode}, |
| 40 | + {"create file", ErrCreateFile(testErr, "/tmp/file"), ErrCreateFileCode}, |
| 41 | + {"create dir", ErrCreateDir(testErr, "/tmp/dir"), ErrCreateDirCode}, |
| 42 | + {"convert to byte", ErrConvertToByte(testErr), ErrConvertToByteCode}, |
| 43 | + {"getting latest release tag", ErrGettingLatestReleaseTag(testErr), ErrGettingLatestReleaseTagCode}, |
| 44 | + {"type cast", ErrTypeCast(testErr), ErrTypeCastCode}, |
| 45 | + {"decode yaml", ErrDecodeYaml(testErr), ErrDecodeYamlCode}, |
| 46 | + {"compress to tar gz", ErrCompressToTarGZ(testErr, "/tmp/file"), ErrCompressToTarGZCode}, |
| 47 | + {"extract tar xz", ErrExtractTarXZ(testErr, "/tmp/archive"), ErrExtractTarXZCode}, |
| 48 | + {"extract zip", ErrExtractZip(testErr, "/tmp/archive"), ErrExtractZipCode}, |
| 49 | + {"read dir", ErrReadDir(testErr, "/tmp/dir"), ErrReadDirCode}, |
| 50 | + {"file walk dir", ErrFileWalkDir(testErr, "/tmp/dir"), ErrFileWalkDirCode}, |
| 51 | + {"relative path", ErrRelPath(testErr, "/tmp/dir"), ErrRelPathCode}, |
| 52 | + {"copy file", ErrCopyFile(testErr), ErrCopyFileCode}, |
| 53 | + {"close file", ErrCloseFile(testErr), ErrCloseFileCode}, |
| 54 | + {"open file", ErrOpenFile("/tmp/file"), ErrOpenFileCode}, |
| 55 | + {"google jwt invalid", ErrGoogleJwtInvalid(testErr), ErrGoogleJwtInvalidCode}, |
| 56 | + {"google sheet service", ErrGoogleSheetSRV(testErr), ErrGoogleSheetSRVCode}, |
| 57 | + {"writing into file", ErrWritingIntoFile(testErr, "artifact"), ErrWritingIntoFileCode}, |
| 58 | + {"invalid protocol", ErrInvalidProtocol, ErrInvalidProtocolCode}, |
| 59 | + {"extract type", ErrExtractType, ErrUnmarshalTypeCode}, |
| 60 | + {"invalid schema version", ErrInvalidSchemaVersion, ErrInvalidSchemaVersionCode}, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tc := range testCases { |
| 64 | + t.Run(tc.name, func(t *testing.T) { |
| 65 | + if got := meshkiterrors.GetCode(tc.err); got != tc.code { |
| 66 | + t.Fatalf("expected error code %q, got %q", tc.code, got) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
0 commit comments