|
| 1 | +package error |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/go-multierror" |
| 8 | +) |
| 9 | + |
| 10 | +const referenceTemplate = "https://github.com/opencontainers/runtime-spec/blob/v%s/%s" |
| 11 | + |
| 12 | +// SpecErrorCode represents the compliance content. |
| 13 | +type SpecErrorCode int |
| 14 | + |
| 15 | +const ( |
| 16 | + // NonError represents that an input is not an error |
| 17 | + NonError SpecErrorCode = iota |
| 18 | + // NonRFCError represents that an error is not a rfc2119 error |
| 19 | + NonRFCError |
| 20 | + |
| 21 | + // ConfigFileExistence represents the error code of 'config.json' existence test |
| 22 | + ConfigFileExistence |
| 23 | + // ArtifactsInSingleDir represents the error code of artifacts place test |
| 24 | + ArtifactsInSingleDir |
| 25 | + |
| 26 | + // SpecVersion represents the error code of specfication version test |
| 27 | + SpecVersion |
| 28 | + |
| 29 | + // RootOnNonHyperV represents the error code of root setting test on non hyper-v containers |
| 30 | + RootOnNonHyperV |
| 31 | + // RootOnHyperV represents the error code of root setting test on hyper-v containers |
| 32 | + RootOnHyperV |
| 33 | + // PathFormatOnWindows represents the error code of the path format test on Window |
| 34 | + PathFormatOnWindows |
| 35 | + // PathName represents the error code of the path name test |
| 36 | + PathName |
| 37 | + // PathExistence represents the error code of the path existence test |
| 38 | + PathExistence |
| 39 | + // ReadonlyFilesystem represents the error code of readonly test |
| 40 | + ReadonlyFilesystem |
| 41 | + // ReadonlyOnWindows represents the error code of readonly setting test on Windows |
| 42 | + ReadonlyOnWindows |
| 43 | + |
| 44 | + // DefaultFilesystems represents the error code of default filesystems test |
| 45 | + DefaultFilesystems |
| 46 | +) |
| 47 | + |
| 48 | +type errorTemplate struct { |
| 49 | + Level Level |
| 50 | + Reference func(version string) (reference string, err error) |
| 51 | +} |
| 52 | + |
| 53 | +var ( |
| 54 | + containerFormatRef = func(version string) (reference string, err error) { |
| 55 | + return fmt.Sprintf(referenceTemplate, version, "bundle.md#container-format"), nil |
| 56 | + } |
| 57 | + specVersionRef = func(version string) (reference string, err error) { |
| 58 | + return fmt.Sprintf(referenceTemplate, version, "config.md#specification-version"), nil |
| 59 | + } |
| 60 | + rootRef = func(version string) (reference string, err error) { |
| 61 | + return fmt.Sprintf(referenceTemplate, version, "config.md#root"), nil |
| 62 | + } |
| 63 | + defaultFSRef = func(version string) (reference string, err error) { |
| 64 | + return fmt.Sprintf(referenceTemplate, version, "config-linux.md#default-filesystems"), nil |
| 65 | + } |
| 66 | +) |
| 67 | + |
| 68 | +var ociErrors = map[SpecErrorCode]errorTemplate{ |
| 69 | + // Bundle.md |
| 70 | + // Container Format |
| 71 | + ConfigFileExistence: errorTemplate{Level: Must, Reference: containerFormatRef}, |
| 72 | + ArtifactsInSingleDir: errorTemplate{Level: Must, Reference: containerFormatRef}, |
| 73 | + |
| 74 | + // Config.md |
| 75 | + // Specification Version |
| 76 | + SpecVersion: errorTemplate{Level: Must, Reference: specVersionRef}, |
| 77 | + // Root |
| 78 | + RootOnNonHyperV: errorTemplate{Level: Required, Reference: rootRef}, |
| 79 | + RootOnHyperV: errorTemplate{Level: Must, Reference: rootRef}, |
| 80 | + // TODO: add tests for 'PathFormatOnWindows' |
| 81 | + PathFormatOnWindows: errorTemplate{Level: Must, Reference: rootRef}, |
| 82 | + PathName: errorTemplate{Level: Should, Reference: rootRef}, |
| 83 | + PathExistence: errorTemplate{Level: Must, Reference: rootRef}, |
| 84 | + ReadonlyFilesystem: errorTemplate{Level: Must, Reference: rootRef}, |
| 85 | + ReadonlyOnWindows: errorTemplate{Level: Must, Reference: rootRef}, |
| 86 | + |
| 87 | + // Config-Linux.md |
| 88 | + // Default Filesystems |
| 89 | + DefaultFilesystems: errorTemplate{Level: Should, Reference: defaultFSRef}, |
| 90 | +} |
| 91 | + |
| 92 | +// NewError creates an Error referencing a spec violation. The error |
| 93 | +// can be cast to a *runtime-tools.error.Error for extracting |
| 94 | +// structured information about the level of the violation and a |
| 95 | +// reference to the violated spec condition. |
| 96 | +// |
| 97 | +// A version string (for the version of the spec that was violated) |
| 98 | +// must be set to get a working URL. |
| 99 | +func NewError(code SpecErrorCode, msg string, version string) (err error) { |
| 100 | + template := ociErrors[code] |
| 101 | + reference, err := template.Reference(version) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + return &Error{ |
| 106 | + Level: template.Level, |
| 107 | + Reference: reference, |
| 108 | + Err: errors.New(msg), |
| 109 | + ErrCode: int(code), |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// FindError finds an error from a source error (mulitple error) and |
| 114 | +// returns the error code if founded. |
| 115 | +// If the source error is nil or empty, return NonError. |
| 116 | +// If the source error is not a multiple error, return NonRFCError. |
| 117 | +func FindError(err error, code SpecErrorCode) SpecErrorCode { |
| 118 | + if err == nil { |
| 119 | + return NonError |
| 120 | + } |
| 121 | + |
| 122 | + if merr, ok := err.(*multierror.Error); ok { |
| 123 | + if merr.ErrorOrNil() == nil { |
| 124 | + return NonError |
| 125 | + } |
| 126 | + for _, e := range merr.Errors { |
| 127 | + if rfcErr, ok := e.(*Error); ok { |
| 128 | + if rfcErr.ErrCode == int(code) { |
| 129 | + return code |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return NonRFCError |
| 135 | +} |
0 commit comments