dimanche 7 juin 2020

Error handling design - when errors need to be handled at top of function stack according to error types, but info needs to be added along the stack

I need to refactor code in Go, so that different types of errors that propagate to the main funtion will result in different actions to handle them. In any case the error message should be logged.

I would usually just have different types of errors, and have them derived from base error classes, each corresponding to a type of action I want to take. This approch does not work for this code, as the errors keep getting re-written, in order to add more context to the error string.

E.g. -

func start_something(...)(...){
    var, err = get_something()
     if err != nil {
          return fmt.Errorf("unable to start something: %w", err.string())
     }
}

I could just add magic strings to the errors, and then parse the error messages in main. This feels too ugly to do. Will appreciate any suggested direction in how to write this. Especially from people with some experience writting Go, that could direct me to the correct Go way to do it.

Thank you,

Aucun commentaire:

Enregistrer un commentaire