This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
121 lines (106 loc) · 2.67 KB
/
errors.go
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
121
package pouchdb
import (
"github.com/gopherjs/gopherjs/js"
)
// PouchError records an error returned by the PouchDB library
type PouchError struct {
e *js.Error
Status int `js:"status"`
Message string `js:"message"`
Name string `js:"name"`
IsError bool `js:"isError"`
Reason string `js:"reason"`
}
// Error satisfies the error interface for the PouchError type
func (e *PouchError) Error() string {
if e.Reason != "" && e.Reason != "undefined" && e.Reason != e.Message {
return e.Message + ": " + e.Reason
}
return e.Message
}
// ErrorStatus returns the status of a PouchError, or 0 for other errors
func ErrorStatus(err error) int {
switch pe := err.(type) {
case *PouchError:
return pe.Status
}
return 0
}
// ErrorMessage returns the message portion of a PouchError, or "" for other errors
func ErrorMessage(err error) string {
switch pe := err.(type) {
case *PouchError:
return pe.Message
}
return ""
}
// ErrorName returns the name portion of a PouchError, or "" for other errors
func ErrorName(err error) string {
switch pe := err.(type) {
case *PouchError:
return pe.Name
}
return ""
}
// ErrorReason returns the reason portion of a PouchError, or "" for other errors
func ErrorReason(err error) string {
switch pe := err.(type) {
case *PouchError:
return pe.Reason
}
return ""
}
// IsNotExist returns true if the passed error represents a PouchError with
// a status of 404 (not found)
func IsNotExist(err error) bool {
switch pe := err.(type) {
case *PouchError:
return pe.Status == 404
}
return false
}
// IsConflict returns true if the passed error is a PouchError with a status
// of 409 (conflict)
func IsConflict(err error) bool {
switch pe := err.(type) {
case *PouchError:
return pe.Status == 409
}
return false
}
// IsPouchError returns true if the passed error is a PouchError, false
// if it is any other type of error.
func IsPouchError(err error) bool {
switch err.(type) {
case *PouchError:
return true
}
return false
}
// NewPouchError creates a new PouchError from a js.Error object returned from the PouchDB library
func NewPouchError(err *js.Error) error {
if err == nil {
return nil
}
return &PouchError{e: err}
}
// Underlying returns the underlying js.Error object, as returned from the PouchDB library
func (e *PouchError) Underlying() *js.Error {
return e.e
}
// Warning represents a non-fatal PouchDB warning.
type Warning struct {
Message string
}
func (w *Warning) Error() string {
return "WARNING: " + w.Message
}
// IsWarning returns true of the error message is a PouchDB warning, otherwise
// false.
func IsWarning(err error) bool {
switch err.(type) {
case *Warning:
return true
}
return false
}