forked from opencontainers/runtime-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oci error: add error level and reference
Signed-off-by: liangchenye <liangchenye@huawei.com>
- Loading branch information
1 parent
ca03d44
commit 4029999
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package validate | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ComplianceLevel represents the OCI compliance levels | ||
type ComplianceLevel int | ||
|
||
const ( | ||
ComplianceOptional ComplianceLevel = iota | ||
ComplianceMay | ||
ComplianceRecommended | ||
ComplianceShould | ||
ComplianceShouldNot | ||
ComplianceShall | ||
ComplianceShallNot | ||
ComplianceRequired | ||
ComplianceMustNot | ||
ComplianceMust | ||
) | ||
|
||
// OCIErrorCode represents the compliance content | ||
type OCIErrorCode int | ||
|
||
const ( | ||
DefaultFilesystems OCIErrorCode = iota | ||
) | ||
|
||
// OCIError represents an error with compliance level and OCI reference | ||
type OCIError struct { | ||
Level ComplianceLevel | ||
Reference string | ||
Err error | ||
} | ||
|
||
//FIXME: change to tagged spec releases | ||
const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob/master/" | ||
|
||
var ociErrors = map[OCIErrorCode]OCIError{ | ||
DefaultFilesystems: OCIError{Level: ComplianceShould, Reference: "config-linux.md#default-filesystems"}, | ||
} | ||
|
||
// ParseLevel takes a string level and returns the OCI compliance level constant | ||
func ParseLevel(level string) ComplianceLevel { | ||
switch strings.ToUpper(level) { | ||
case "SHOULD": | ||
return ComplianceShould | ||
case "MUST": | ||
return ComplianceMust | ||
default: | ||
return ComplianceMust | ||
} | ||
} | ||
|
||
// NewOCIError creates an OCIError by OCIErrorCode and message | ||
func NewOCIError(code OCIErrorCode, msg string) error { | ||
err := ociErrors[code] | ||
err.Err = errors.New(msg) | ||
|
||
return &err | ||
} | ||
|
||
// Error returns the error message with OCI reference | ||
func (oci *OCIError) Error() string { | ||
return fmt.Sprintf("%s\nRefer to: %s%s", oci.Err.Error(), referencePrefix, oci.Reference) | ||
} |