Skip to content
Snippets Groups Projects
Select Git revision
  • 4b385855e31c008f47ae7f765b1760cefe9fc769
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

error.go

Blame
  • error.go 5.80 KiB
    package errors
    
    import (
    	"fmt"
    	"path"
    	"strconv"
    	"strings"
    )
    
    //CustomError implements the following interfaces:
    //	error
    //	github.com/pkg/errors: Cause
    type CustomError struct {
    	code    int
    	message string
    	caller  Caller
    	cause   error
    }
    
    //implement interface error:
    func (err CustomError) Error() string {
    	return err.Formatted(FormattingOptions{Causes: false})
    }
    
    func Is(e1, e2 error) bool {
    	if e1WithIs, ok := e1.(ErrorWithIs); ok {
    		return e1WithIs.Is(e2)
    	}
    	return e1.Error() == e2.Error()
    }
    
    //Is() compares the message string of this or any cause to match the specified error message
    func (err CustomError) Is(specificError error) bool {
    	if err.message == specificError.Error() {
    		return true
    	}
    	if err.cause != nil {
    		if causeWithIs, ok := err.cause.(ErrorWithIs); ok {
    			return causeWithIs.Is(specificError)
    		}
    	}
    	return false
    }
    
    //implement github.com/pkg/errors: Cause
    func (err CustomError) Cause() error {
    	return err.cause
    }
    
    func HTTPCode(err error) int {
    	if errWithCode, ok := err.(ErrorWithCause); ok {
    		return errWithCode.Code()
    	}
    	return 0
    }
    
    func (err CustomError) Code() int {
    	//find http error code - returning the smallest code in the stack of causes (excluding code==0)
    	code := err.code
    	if err.cause != nil {
    		if causeWithCode, ok := err.cause.(ErrorWithCause); ok {
    			causeCode := causeWithCode.Code()
    			if code == 0 || (causeCode != 0 && causeCode < code) {
    				code = causeCode
    			}
    		}
    	}
    	return code
    }