github.com/cockroachdb/errors@v1.11.1/errbase/adapters_errno.go (about) 1 // Copyright 2019 The Cockroach 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 // http://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 12 // implied. See the License for the specific language governing 13 // permissions and limitations under the License. 14 15 // +build !plan9 16 17 package errbase 18 19 import ( 20 "context" 21 "os" 22 "runtime" 23 "syscall" 24 25 "github.com/cockroachdb/errors/errorspb" 26 "github.com/gogo/protobuf/proto" 27 ) 28 29 const thisArch = runtime.GOOS + ":" + runtime.GOARCH 30 31 func encodeErrno(_ context.Context, err error) (msg string, safe []string, payload proto.Message) { 32 e := err.(syscall.Errno) 33 payload = &errorspb.ErrnoPayload{ 34 OrigErrno: int64(e), 35 Arch: thisArch, 36 IsPermission: e.Is(os.ErrPermission), 37 IsExist: e.Is(os.ErrExist), 38 IsNotExist: e.Is(os.ErrNotExist), 39 IsTimeout: e.Timeout(), 40 IsTemporary: e.Temporary(), 41 } 42 return e.Error(), []string{e.Error()}, payload 43 } 44 45 func decodeErrno(_ context.Context, msg string, _ []string, payload proto.Message) error { 46 m, ok := payload.(*errorspb.ErrnoPayload) 47 if !ok { 48 // If this ever happens, this means some version of the library 49 // (presumably future) changed the payload type, and we're 50 // receiving this here. In this case, give up and let 51 // DecodeError use the opaque type. 52 return nil 53 } 54 if m.Arch != thisArch { 55 // The errno object is coming from a different platform. We'll 56 // keep it opaque here. 57 return &OpaqueErrno{msg: msg, details: m} 58 } 59 return syscall.Errno(m.OrigErrno) 60 } 61 62 func init() { 63 pKey := GetTypeKey(syscall.Errno(0)) 64 RegisterLeafEncoder(pKey, encodeErrno) 65 RegisterLeafDecoder(pKey, decodeErrno) 66 }