depot/web/fup/fuphttp/errors.go

47 lines
743 B
Go
Raw Normal View History

2021-03-21 15:34:50 +00:00
// SPDX-FileCopyrightText: 2021 Luke Granger-Brown <depot@lukegb.com>
//
// SPDX-License-Identifier: Apache-2.0
package fuphttp
import (
"errors"
"fmt"
"gocloud.dev/gcerrors"
)
type fupError struct {
Code gcerrors.ErrorCode
msg string
err error
}
func (e *fupError) Error() string {
var msg string
if e.msg == "" {
msg = fmt.Sprintf("code=%v", e.Code)
} else {
msg = fmt.Sprintf("%s (code=%v)", e.msg, e.Code)
}
if e.err != nil {
msg = fmt.Sprintf("%s: %s", msg, e.err)
}
return msg
}
func (e *fupError) Unwrap() error {
return e.err
}
func errorCode(err error) gcerrors.ErrorCode {
if err == nil {
return gcerrors.OK
}
var e *fupError
if errors.As(err, &e) {
return e.Code
}
return gcerrors.Code(err)
}