github.com/thiagoyeds/go-cloud@v0.26.0/gcerrors/errors.go (about) 1 // Copyright 2019 The Go Cloud Development Kit Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package gcerrors provides support for getting error codes from 16 // errors returned by Go CDK APIs. 17 package gcerrors 18 19 import ( 20 "context" 21 22 "gocloud.dev/internal/gcerr" 23 "golang.org/x/xerrors" 24 ) 25 26 // An ErrorCode describes the error's category. Programs should act upon an error's 27 // code, not its message. 28 type ErrorCode = gcerr.ErrorCode 29 30 const ( 31 // Returned by the Code function on a nil error. It is not a valid 32 // code for an error. 33 OK ErrorCode = gcerr.OK 34 35 // The error could not be categorized. 36 Unknown ErrorCode = gcerr.Unknown 37 38 // The resource was not found. 39 NotFound ErrorCode = gcerr.NotFound 40 41 // The resource exists, but it should not. 42 AlreadyExists ErrorCode = gcerr.AlreadyExists 43 44 // A value given to a Go CDK API is incorrect. 45 InvalidArgument ErrorCode = gcerr.InvalidArgument 46 47 // Something unexpected happened. Internal errors always indicate 48 // bugs in the Go CDK (or possibly the underlying service). 49 Internal ErrorCode = gcerr.Internal 50 51 // The feature is not implemented. 52 Unimplemented ErrorCode = gcerr.Unimplemented 53 54 // The system was in the wrong state. 55 FailedPrecondition ErrorCode = gcerr.FailedPrecondition 56 57 // The caller does not have permission to execute the specified operation. 58 PermissionDenied ErrorCode = gcerr.PermissionDenied 59 60 // Some resource has been exhausted, typically because a service resource limit 61 // has been reached. 62 ResourceExhausted ErrorCode = gcerr.ResourceExhausted 63 64 // The operation was canceled. 65 Canceled ErrorCode = gcerr.Canceled 66 67 // The operation timed out. 68 DeadlineExceeded ErrorCode = gcerr.DeadlineExceeded 69 ) 70 71 // Code returns the ErrorCode of err if it, or some error it wraps, is an *Error. 72 // If err is context.Canceled or context.DeadlineExceeded, or wraps one of those errors, 73 // it returns the Canceled or DeadlineExceeded codes, respectively. 74 // If err is nil, it returns the special code OK. 75 // Otherwise, it returns Unknown. 76 func Code(err error) ErrorCode { 77 if err == nil { 78 return OK 79 } 80 var e *gcerr.Error 81 if xerrors.As(err, &e) { 82 return e.Code 83 } 84 if xerrors.Is(err, context.Canceled) { 85 return Canceled 86 } 87 if xerrors.Is(err, context.DeadlineExceeded) { 88 return DeadlineExceeded 89 } 90 return Unknown 91 }