11package models
22
3+ import "errors"
4+
5+ // sentinel error for all not found errors.
6+ var errNotFound = errors .New ("not found" )
7+
8+ // sentinel error for unique constraint violations.
9+ var errUniqueConstraintViolated = errors .New ("unique constraint violated" )
10+
311// IsNotFoundError returns whether an error represents a "not found" error.
412func IsNotFoundError (err error ) bool {
5- switch err .(type ) {
6- case UserNotFoundError , * UserNotFoundError :
7- return true
8- case SessionNotFoundError , * SessionNotFoundError :
9- return true
10- case ConfirmationTokenNotFoundError , * ConfirmationTokenNotFoundError :
11- return true
12- case ConfirmationOrRecoveryTokenNotFoundError , * ConfirmationOrRecoveryTokenNotFoundError :
13- return true
14- case RefreshTokenNotFoundError , * RefreshTokenNotFoundError :
15- return true
16- case IdentityNotFoundError , * IdentityNotFoundError :
17- return true
18- case ChallengeNotFoundError , * ChallengeNotFoundError :
19- return true
20- case FactorNotFoundError , * FactorNotFoundError :
21- return true
22- case SSOProviderNotFoundError , * SSOProviderNotFoundError :
23- return true
24- case SAMLRelayStateNotFoundError , * SAMLRelayStateNotFoundError :
25- return true
26- case FlowStateNotFoundError , * FlowStateNotFoundError :
27- return true
28- case OneTimeTokenNotFoundError , * OneTimeTokenNotFoundError :
29- return true
30- case OAuthServerClientNotFoundError , * OAuthServerClientNotFoundError :
31- return true
32- case OAuthServerAuthorizationNotFoundError , * OAuthServerAuthorizationNotFoundError :
33- return true
34- case OAuthClientStateNotFoundError , * OAuthClientStateNotFoundError :
35- return true
36- case CustomOAuthProviderNotFoundError , * CustomOAuthProviderNotFoundError :
37- return true
38- }
39- return false
13+ return errors .Is (err , errNotFound )
4014}
4115
4216type SessionNotFoundError struct {}
@@ -45,55 +19,87 @@ func (e SessionNotFoundError) Error() string {
4519 return "Session not found"
4620}
4721
22+ func (e SessionNotFoundError ) Is (target error ) bool {
23+ return target == errNotFound
24+ }
25+
4826// UserNotFoundError represents when a user is not found.
4927type UserNotFoundError struct {}
5028
5129func (e UserNotFoundError ) Error () string {
5230 return "User not found"
5331}
5432
33+ func (e UserNotFoundError ) Is (target error ) bool {
34+ return target == errNotFound
35+ }
36+
5537// IdentityNotFoundError represents when an identity is not found.
5638type IdentityNotFoundError struct {}
5739
5840func (e IdentityNotFoundError ) Error () string {
5941 return "Identity not found"
6042}
6143
44+ func (e IdentityNotFoundError ) Is (target error ) bool {
45+ return target == errNotFound
46+ }
47+
6248// ConfirmationOrRecoveryTokenNotFoundError represents when a confirmation or recovery token is not found.
6349type ConfirmationOrRecoveryTokenNotFoundError struct {}
6450
6551func (e ConfirmationOrRecoveryTokenNotFoundError ) Error () string {
6652 return "Confirmation or Recovery Token not found"
6753}
6854
55+ func (e ConfirmationOrRecoveryTokenNotFoundError ) Is (target error ) bool {
56+ return target == errNotFound
57+ }
58+
6959// ConfirmationTokenNotFoundError represents when a confirmation token is not found.
7060type ConfirmationTokenNotFoundError struct {}
7161
7262func (e ConfirmationTokenNotFoundError ) Error () string {
7363 return "Confirmation Token not found"
7464}
7565
66+ func (e ConfirmationTokenNotFoundError ) Is (target error ) bool {
67+ return target == errNotFound
68+ }
69+
7670// RefreshTokenNotFoundError represents when a refresh token is not found.
7771type RefreshTokenNotFoundError struct {}
7872
7973func (e RefreshTokenNotFoundError ) Error () string {
8074 return "Refresh Token not found"
8175}
8276
77+ func (e RefreshTokenNotFoundError ) Is (target error ) bool {
78+ return target == errNotFound
79+ }
80+
8381// FactorNotFoundError represents when a user is not found.
8482type FactorNotFoundError struct {}
8583
8684func (e FactorNotFoundError ) Error () string {
8785 return "Factor not found"
8886}
8987
88+ func (e FactorNotFoundError ) Is (target error ) bool {
89+ return target == errNotFound
90+ }
91+
9092// ChallengeNotFoundError represents when a user is not found.
9193type ChallengeNotFoundError struct {}
9294
9395func (e ChallengeNotFoundError ) Error () string {
9496 return "Challenge not found"
9597}
9698
99+ func (e ChallengeNotFoundError ) Is (target error ) bool {
100+ return target == errNotFound
101+ }
102+
97103// SSOProviderNotFoundError represents an error when a SSO Provider can't be
98104// found.
99105type SSOProviderNotFoundError struct {}
@@ -102,6 +108,10 @@ func (e SSOProviderNotFoundError) Error() string {
102108 return "SSO Identity Provider not found"
103109}
104110
111+ func (e SSOProviderNotFoundError ) Is (target error ) bool {
112+ return target == errNotFound
113+ }
114+
105115// SAMLRelayStateNotFoundError represents an error when a SAML relay state
106116// can't be found.
107117type SAMLRelayStateNotFoundError struct {}
@@ -110,6 +120,10 @@ func (e SAMLRelayStateNotFoundError) Error() string {
110120 return "SAML RelayState not found"
111121}
112122
123+ func (e SAMLRelayStateNotFoundError ) Is (target error ) bool {
124+ return target == errNotFound
125+ }
126+
113127// FlowStateNotFoundError represents an error when an FlowState can't be
114128// found.
115129type FlowStateNotFoundError struct {}
@@ -118,12 +132,12 @@ func (e FlowStateNotFoundError) Error() string {
118132 return "Flow State not found"
119133}
120134
135+ func (e FlowStateNotFoundError ) Is (target error ) bool {
136+ return target == errNotFound
137+ }
138+
121139func IsUniqueConstraintViolatedError (err error ) bool {
122- switch err .(type ) {
123- case UserEmailUniqueConflictError , * UserEmailUniqueConflictError :
124- return true
125- }
126- return false
140+ return errors .Is (err , errUniqueConstraintViolated )
127141}
128142
129143type UserEmailUniqueConflictError struct {}
@@ -132,15 +146,27 @@ func (e UserEmailUniqueConflictError) Error() string {
132146 return "User email unique constraint violated"
133147}
134148
149+ func (e UserEmailUniqueConflictError ) Is (target error ) bool {
150+ return target == errUniqueConstraintViolated
151+ }
152+
135153type OAuthClientStateNotFoundError struct {}
136154
137155func (e OAuthClientStateNotFoundError ) Error () string {
138156 return "OAuth state not found"
139157}
140158
159+ func (e OAuthClientStateNotFoundError ) Is (target error ) bool {
160+ return target == errNotFound
161+ }
162+
141163// CustomOAuthProviderNotFoundError represents an error when a custom OAuth/OIDC provider can't be found
142164type CustomOAuthProviderNotFoundError struct {}
143165
144166func (e CustomOAuthProviderNotFoundError ) Error () string {
145167 return "Custom OAuth provider not found"
146168}
169+
170+ func (e CustomOAuthProviderNotFoundError ) Is (target error ) bool {
171+ return target == errNotFound
172+ }
0 commit comments