File: errors.go

package info (click to toggle)
golang-github-dsnet-compress 0.0.2~git20230904.39efe44%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,724 kB
  • sloc: sh: 108; makefile: 5
file content (120 lines) | stat: -rw-r--r-- 3,703 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

// Package errors implements functions to manipulate compression errors.
//
// In idiomatic Go, it is an anti-pattern to use panics as a form of error
// reporting in the API. Instead, the expected way to transmit errors is by
// returning an error value. Unfortunately, the checking of "err != nil" in
// tight loops commonly found in compression causes non-negligible performance
// degradation. While this may not be idiomatic, the internal packages of this
// repository rely on panics as a normal means to convey errors. In order to
// ensure that these panics do not leak across the public API, the public
// packages must recover from these panics and present an error value.
//
// The Panic and Recover functions in this package provide a safe way to
// recover from errors only generated from within this repository.
//
// Example usage:
//
//	func Foo() (err error) {
//		defer errors.Recover(&err)
//
//		if rand.Intn(2) == 0 {
//			// Unexpected panics will not be caught by Recover.
//			io.Closer(nil).Close()
//		} else {
//			// Errors thrown by Panic will be caught by Recover.
//			errors.Panic(errors.New("whoopsie"))
//		}
//	}
package errors

import "strings"

const (
	// Unknown indicates that there is no classification for this error.
	Unknown = iota

	// Internal indicates that this error is due to an internal bug.
	// Users should file a issue report if this type of error is encountered.
	Internal

	// Invalid indicates that this error is due to the user misusing the API
	// and is indicative of a bug on the user's part.
	Invalid

	// Deprecated indicates the use of a deprecated and unsupported feature.
	Deprecated

	// Corrupted indicates that the input stream is corrupted.
	Corrupted

	// Closed indicates that the handlers are closed.
	Closed
)

var codeMap = map[int]string{
	Unknown:    "unknown error",
	Internal:   "internal error",
	Invalid:    "invalid argument",
	Deprecated: "deprecated format",
	Corrupted:  "corrupted input",
	Closed:     "closed handler",
}

type Error struct {
	Code int    // The error type
	Pkg  string // Name of the package where the error originated
	Msg  string // Descriptive message about the error (optional)
}

func (e Error) Error() string {
	var ss []string
	for _, s := range []string{e.Pkg, codeMap[e.Code], e.Msg} {
		if s != "" {
			ss = append(ss, s)
		}
	}
	return strings.Join(ss, ": ")
}

func (e Error) CompressError()     {}
func (e Error) IsInternal() bool   { return e.Code == Internal }
func (e Error) IsInvalid() bool    { return e.Code == Invalid }
func (e Error) IsDeprecated() bool { return e.Code == Deprecated }
func (e Error) IsCorrupted() bool  { return e.Code == Corrupted }
func (e Error) IsClosed() bool     { return e.Code == Closed }

func IsInternal(err error) bool   { return isCode(err, Internal) }
func IsInvalid(err error) bool    { return isCode(err, Invalid) }
func IsDeprecated(err error) bool { return isCode(err, Deprecated) }
func IsCorrupted(err error) bool  { return isCode(err, Corrupted) }
func IsClosed(err error) bool     { return isCode(err, Closed) }

func isCode(err error, code int) bool {
	if cerr, ok := err.(Error); ok && cerr.Code == code {
		return true
	}
	return false
}

// errWrap is used by Panic and Recover to ensure that only errors raised by
// Panic are recovered by Recover.
type errWrap struct{ e *error }

func Recover(err *error) {
	switch ex := recover().(type) {
	case nil:
		// Do nothing.
	case errWrap:
		*err = *ex.e
	default:
		panic(ex)
	}
}

func Panic(err error) {
	panic(errWrap{&err})
}