github.com/go-kivik/kivik/v4@v4.3.2/x/fsdb/cdb/errors.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package cdb 14 15 import ( 16 "errors" 17 "net/http" 18 "os" 19 ) 20 21 var ( 22 errNotFound = statusError{status: http.StatusNotFound, error: errors.New("missing")} 23 errUnrecognizedFile = errors.New("unrecognized file") 24 errConflict = statusError{status: http.StatusConflict, error: errors.New("document update conflict")} 25 ) 26 27 // missing transforms a NotExist error into a standard CouchDBesque 'missing' 28 // error. All other values are passed through unaltered. 29 func missing(err error) error { 30 if !os.IsNotExist(err) { 31 return err 32 } 33 return errNotFound 34 } 35 36 func kerr(err error) error { 37 if err == nil { 38 return nil 39 } 40 if errors.Is(err, &statusError{}) { 41 // Error has already been converted 42 return err 43 } 44 if os.IsNotExist(err) { 45 return statusError{status: http.StatusNotFound, error: err} 46 } 47 if os.IsPermission(err) { 48 return statusError{status: http.StatusForbidden, error: err} 49 } 50 return err 51 } 52 53 type statusError struct { 54 error 55 status int 56 } 57 58 func (e statusError) Unwrap() error { return e.error } 59 func (e statusError) HTTPStatus() int { return e.status }