github.com/iDigitalFlame/xmt@v0.5.4/util/xerr/z_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  import "github.com/iDigitalFlame/xmt/util"
    34  
    35  // ExtendedInfo is a compile time constant to help signal if complex string
    36  // values should be concatenated inline.
    37  //
    38  // This helps prevent debugging when the "-tags implant" option is enabled.
    39  const ExtendedInfo = false
    40  
    41  type numErr uint8
    42  
    43  func (e numErr) Error() string {
    44  	return "0x" + byteHexStr(e)
    45  }
    46  func byteHexStr(b numErr) string {
    47  	if b == 0 {
    48  		return "0"
    49  	}
    50  	if b < 16 {
    51  		return util.HexTable[b&0x0F : (b&0x0F)+1]
    52  	}
    53  	return util.HexTable[b>>4:(b>>4)+1] + util.HexTable[b&0x0F:(b&0x0F)+1]
    54  }
    55  
    56  // Sub creates a new string backed error interface and returns it.
    57  // This error struct does not support Unwrapping.
    58  //
    59  // If the "-tags implant" option is selected, the second value, the error code,
    60  // will be used instead, otherwise it's ignored.
    61  //
    62  // The resulting errors created will be comparable.
    63  func Sub(_ string, c uint8) error {
    64  	return numErr(c)
    65  }
    66  
    67  // Wrap creates a new error that wraps the specified error.
    68  //
    69  // If not nil, this function will append ": " + 'Error()' to the resulting
    70  // string message and will keep the original error for unwrapping.
    71  //
    72  // If "-tags implant" is specified, this will instead return the wrapped error
    73  // directly.
    74  func Wrap(_ string, e error) error {
    75  	return e
    76  }