| 1 |
// SPDX-License-Identifier: BSD-3-Clause |
| 2 |
|
| 3 |
//go:build linux && !go1.20 |
| 4 |
|
| 5 |
// Copyright (C) 2024 SUSE LLC. All rights reserved. |
| 6 |
// Use of this source code is governed by a BSD-style |
| 7 |
// license that can be found in the LICENSE file. |
| 8 |
|
| 9 |
package gocompat |
| 10 |
|
| 11 |
import ( |
| 12 |
"fmt" |
| 13 |
) |
| 14 |
|
| 15 |
type wrappedError struct { |
| 16 |
inner error |
| 17 |
isError error |
| 18 |
} |
| 19 |
|
| 20 |
func (err wrappedError) Is(target error) bool { |
| 21 |
return err.isError == target |
| 22 |
} |
| 23 |
|
| 24 |
func (err wrappedError) Unwrap() error { |
| 25 |
return err.inner |
| 26 |
} |
| 27 |
|
| 28 |
func (err wrappedError) Error() string { |
| 29 |
return fmt.Sprintf("%v: %v", err.isError, err.inner) |
| 30 |
} |
| 31 |
|
| 32 |
// WrapBaseError is a helper that is equivalent to fmt.Errorf("%w: %w"), except |
| 33 |
// that on pre-1.20 Go versions only errors.Is() works properly (errors.Unwrap) |
| 34 |
// is only guaranteed to give you baseErr. |
| 35 |
func WrapBaseError(baseErr, extraErr error) error { |
| 36 |
return wrappedError{ |
| 37 |
inner: baseErr, |
| 38 |
isError: extraErr, |
| 39 |
} |
| 40 |
} |