github.com/iDigitalFlame/xmt@v0.5.4/util/xerr/z_no_implant.go (about) 1 //go:build !implant 2 // +build !implant 3 4 // Copyright (C) 2020 - 2023 iDigitalFlame 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 // 19 20 // Package xerr is a simplistic (and more efficient) re-write of the "errors" 21 // built-in package. 22 // 23 // This is used to create comparable and (sometimes) un-wrapable error structs. 24 // 25 // This package acts differently when the "implant" build tag is used. If enabled, 26 // Most error string values are stripped to prevent identification and debugging. 27 // 28 // It is recommended if errors are needed to be compared even when in an implant 29 // build, to use the "Sub" function, which will ignore error strings and use 30 // error codes instead. 31 package xerr 32 33 // ExtendedInfo is a compile time constant to help signal if complex string 34 // values should be concatenated inline. 35 // 36 // This helps prevent debugging when the "-tags implant" option is enabled. 37 const ExtendedInfo = true 38 39 type strErr string 40 type wrapErr struct { 41 e error 42 s string 43 } 44 45 // New creates a new string backed error interface and returns it. 46 // This error struct does not support Unwrapping. 47 // 48 // The resulting errors created will be comparable. 49 func New(s string) error { 50 return strErr(s) 51 } 52 func (e strErr) Error() string { 53 return string(e) 54 } 55 func (e wrapErr) Error() string { 56 return e.s 57 } 58 func (e wrapErr) Unwrap() error { 59 return e.e 60 } 61 62 // Sub creates a new string backed error interface and returns it. 63 // This error struct does not support Unwrapping. 64 // 65 // If the "-tags implant" option is selected, the second value, the error code, 66 // will be used instead, otherwise it's ignored. 67 // 68 // The resulting errors created will be comparable. 69 func Sub(s string, _ uint8) error { 70 return strErr(s) 71 } 72 73 // Wrap creates a new error that wraps the specified error. 74 // 75 // If not nil, this function will append ": " + 'Error()' to the resulting 76 // string message and will keep the original error for unwrapping. 77 // 78 // If "-tags implant" is specified, this will instead return the wrapped error 79 // directly. 80 func Wrap(s string, e error) error { 81 if e != nil { 82 return &wrapErr{s: s + ": " + e.Error(), e: e} 83 } 84 return &wrapErr{s: s} 85 }