Skip to content
Snippets Groups Projects
Select Git revision
  • dbcb4eb74924ac5493699a08003d149a6eb14722
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
23 results

UData.php

Blame
  • error.go 5.87 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
    }