gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

error.go (2631B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package gtserror
     19 
     20 import (
     21 	"codeberg.org/gruf/go-errors/v2"
     22 )
     23 
     24 // package private error key type.
     25 type errkey int
     26 
     27 // ErrorType denotes the type of an error, if set.
     28 type ErrorType string
     29 
     30 const (
     31 	// error value keys.
     32 	_ errkey = iota
     33 	statusCodeKey
     34 	notFoundKey
     35 	errorTypeKey
     36 
     37 	// Types returnable from Type(...).
     38 	TypeSMTP ErrorType = "smtp" // smtp (mail)
     39 )
     40 
     41 // StatusCode checks error for a stored status code value. For example
     42 // an error from an outgoing HTTP request may be stored, or an API handler
     43 // expected response status code may be stored.
     44 func StatusCode(err error) int {
     45 	i, _ := errors.Value(err, statusCodeKey).(int)
     46 	return i
     47 }
     48 
     49 // WithStatusCode will wrap the given error to store provided status code,
     50 // returning wrapped error. See StatusCode() for example use-cases.
     51 func WithStatusCode(err error, code int) error {
     52 	return errors.WithValue(err, statusCodeKey, code)
     53 }
     54 
     55 // NotFound checks error for a stored "not found" flag. For example
     56 // an error from an outgoing HTTP request due to DNS lookup.
     57 func NotFound(err error) bool {
     58 	_, ok := errors.Value(err, notFoundKey).(struct{})
     59 	return ok
     60 }
     61 
     62 // SetNotFound will wrap the given error to store a "not found" flag,
     63 // returning wrapped error. See NotFound() for example use-cases.
     64 func SetNotFound(err error) error {
     65 	return errors.WithValue(err, notFoundKey, struct{}{})
     66 }
     67 
     68 // Type checks error for a stored "type" value. For example
     69 // an error from sending an email may set a value of "smtp"
     70 // to indicate this was an SMTP error.
     71 func Type(err error) ErrorType {
     72 	s, _ := errors.Value(err, errorTypeKey).(ErrorType)
     73 	return s
     74 }
     75 
     76 // SetType will wrap the given error to store a "type" value,
     77 // returning wrapped error. See Type() for example use-cases.
     78 func SetType(err error, errType ErrorType) error {
     79 	return errors.WithValue(err, errorTypeKey, errType)
     80 }