Review a generated operation wrapper

from Error wrapping
Go 1.27 advanced 6 min 4 issues to find

Review this generated operation wrapper.

Wrap execute failures with the operation and job ID, preserve the matchable cause, avoid duplicate reporting, and return nil when execution succeeds.

Go
type OpError struct {
    Op  string
    Err error
}

func (e *OpError) Error() string {
    return e.Op + ": " + e.Err.Error()
}

func (e *OpError) Unwrap() error {
    return e.Err
}

func (e *OpError) Is(target error) bool {
    return errors.Is(e.Err, target)
}

func runJob(id string) error {
    err := execute(id)
    if err != nil {
        log.Printf("job %s failed: %v", id, err)
        return &OpError{Op: "run job " + id, Err: err}
    }
    return &OpError{Op: "run job " + id}
}

generated code is illustrative, not from any one model

Open in playground
Report an error