| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package consumererror // import "go.opentelemetry.io/collector/consumer/consumererror" |
| 5 |
|
| 6 |
import "errors" |
| 7 |
|
| 8 |
// permanent is an error that will be always returned if its source |
| 9 |
// receives the same inputs. |
| 10 |
type permanent struct { |
| 11 |
err error |
| 12 |
} |
| 13 |
|
| 14 |
// NewPermanent wraps an error to indicate that it is a permanent error, i.e. an |
| 15 |
// error that will be always returned if its source receives the same inputs. |
| 16 |
func NewPermanent(err error) error { |
| 17 |
return permanent{err: err} |
| 18 |
} |
| 19 |
|
| 20 |
func (p permanent) Error() string { |
| 21 |
return "Permanent error: " + p.err.Error() |
| 22 |
} |
| 23 |
|
| 24 |
// Unwrap returns the wrapped error for functions Is and As in standard package errors. |
| 25 |
func (p permanent) Unwrap() error { |
| 26 |
return p.err |
| 27 |
} |
| 28 |
|
| 29 |
// IsPermanent checks if an error was wrapped with the NewPermanent function, which |
| 30 |
// is used to indicate that a given error will always be returned in the case |
| 31 |
// that its sources receives the same input. |
| 32 |
func IsPermanent(err error) bool { |
| 33 |
if err == nil { |
| 34 |
return false |
| 35 |
} |
| 36 |
return errors.As(err, &permanent{}) |
| 37 |
} |