tommyd456
tommyd456

Reputation: 10673

Handling multiple errors

I have Function 1:

func Function1() {
    if err := Function2(); err != nil {

    }
}

and Function2:

func Function2() error {
    if err := doSomethingThatMightCauseError(); err != nil {
        return errors.New("Error Type 1")
    }

    if err := doSomethingElseThatMightCauseError(); err != nil {
        return errors.New("Error Type 2")
    }
}

How can I detect what type of error has happened (internal, no results found in db etc) and then handle accordingly in Function 1?

Upvotes: 5

Views: 532

Answers (1)

Not_a_Golfer
Not_a_Golfer

Reputation: 49157

You have 3 main options:

  1. string based, i.e. looking into the message. This is of course pretty bad because if you later change one letter in the message, you need to rewrite all the checking code, so I'd avoid it.

  2. If the error messages can stay constant, simply create errors as global variables, and then compare the received error with a known pre-defined one.

For example:

var ErrDB = errors.New("Database Error")
var ErrTimeout = errors.New("Timeout") //or whatever

and then

if err := someFunc(); err != nil {
  switch err {
  case ErrDB:
    //do stuff that needs to be done here
  case ErrTimeout:
    //etc /etc
  }
}
  1. Create a custom error type, since errors are just interfaces, that can have some identifier or other contextual data.

For example:

const (
  ErrDB = 1
  ErrTimeout = 2
  ...
)

type MyError struct {
   Code int
   Message string
}

// This is what you need to be an error
func (e MyError)Error() string {
   return e.Message
}


func NewError(s string, code int) error {
   return MyError{s,code}
}

and then when you return it do something like this:

// Return a MyError with a DB code for db operations
func someFunc() error {
   if err := talkToDB(); err != nil {
      return NewError(err.Error(), ErrDB)
   }
   return nil
}

and when analyzing it:

if err := someFunc(); err != nil {

   // check if this is a MyError
   if me, ok := err.(MyError); ok {
     // now we can check the code
     switch me.Code {
       case ErrDB:
         //handle this
       ....
     }  
   }
}

Upvotes: 5

Related Questions