1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package exterrors2021import (22 "errors"23)2425type TemporaryErr interface {26 Temporary() bool27}2829// IsTemporaryOrUnspec is similar to IsTemporary except that it returns true30// if error does not have a Temporary() method. Basically, it assumes that31// errors are temporary by default compared to IsTemporary that assumes32// errors are permanent by default.33func IsTemporaryOrUnspec(err error) bool {34 var temp TemporaryErr35 if errors.As(err, &temp) {36 return temp.Temporary()37 }38 return true39}4041// IsTemporary returns true whether the passed error object42// have a Temporary() method and it returns true.43func IsTemporary(err error) bool {44 var temp TemporaryErr45 if errors.As(err, &temp) {46 return temp.Temporary()47 }48 return false49}5051type temporaryErr struct {52 err error53 temp bool54}5556func (t temporaryErr) Unwrap() error {57 return t.err58}5960func (t temporaryErr) Error() string {61 return t.err.Error()62}6364func (t temporaryErr) Temporary() bool {65 return t.temp66}6768// WithTemporary wraps the passed error object with the implementation of the69// Temporary() method that will return the specified value.70//71// Original error value can be obtained using errors.Unwrap.72func WithTemporary(err error, temporary bool) error {73 return temporaryErr{err, temporary}74}