Skip to content

Commit a790792

Browse files
committed
refactor: Added an UserError mixin for easier handling on Sentry.
1 parent c6ad1d7 commit a790792

4 files changed

Lines changed: 25 additions & 25 deletions

File tree

lib/model/backend/authentication/providers/email.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class EmailAuthenticationProvider extends AuthenticationProvider {
1717
if ((uri as EmailSentAppLink).isValid) {
1818
if (uri.cancelCode == null) {
1919
return ResultError(
20-
exception: uri.previously ? EmailAlreadySentException() : _NoCancelCodeException(),
20+
exception: uri.previously ? _EmailAlreadySentException() : _NoCancelCodeException(),
2121
);
2222
}
2323
_ref.read(emailConfirmationStateProvider.notifier)._markNeedsConfirmation(uri.email, uri.cancelCode!);
@@ -219,9 +219,9 @@ class _NoEmailToConfirmException extends LocalizableException {
219219
}
220220

221221
/// Triggered when the email has already been sent.
222-
class EmailAlreadySentException extends LocalizableException {
222+
class _EmailAlreadySentException extends LocalizableException with UserError {
223223
/// Creates a new email already sent exception instance.
224-
EmailAlreadySentException()
224+
_EmailAlreadySentException()
225225
: super(
226226
localizedErrorMessage: translations.error.backend.emailAlreadySent,
227227
);

lib/model/backend/authentication/providers/provider.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:open_authenticator/model/backend/user.dart';
1616
import 'package:open_authenticator/model/settings/backend_url.dart';
1717
import 'package:open_authenticator/model/settings/entry.dart';
1818
import 'package:open_authenticator/utils/platform.dart';
19+
import 'package:open_authenticator/utils/result/reporter.dart';
1920
import 'package:open_authenticator/utils/result/result.dart';
2021
import 'package:open_authenticator/utils/shared_preferences_with_prefix.dart';
2122
import 'package:open_authenticator/utils/uri_builder.dart';

lib/model/backend/request/error.dart

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:open_authenticator/i18n/localizable_exception.dart';
22
import 'package:open_authenticator/i18n/translations.g.dart';
3+
import 'package:open_authenticator/utils/result/reporter.dart';
34

45
/// Represents a backend request error.
56
class BackendRequestError extends LocalizableException {
@@ -65,14 +66,14 @@ class BackendRequestError extends LocalizableException {
6566
statusCode: statusCode,
6667
message: message,
6768
);
68-
case InvalidVerificationCodeError.kErrorCode:
69-
return InvalidVerificationCodeError._(
69+
case _InvalidVerificationCodeError.kErrorCode:
70+
return _InvalidVerificationCodeError._(
7071
route: route,
7172
statusCode: statusCode,
7273
message: message,
7374
);
74-
case InvalidAuthorizationCodeError.kErrorCode:
75-
return InvalidAuthorizationCodeError._(
75+
case _InvalidAuthorizationCodeError.kErrorCode:
76+
return _InvalidAuthorizationCodeError._(
7677
route: route,
7778
statusCode: statusCode,
7879
message: message,
@@ -83,8 +84,8 @@ class BackendRequestError extends LocalizableException {
8384
statusCode: statusCode,
8485
message: message,
8586
);
86-
case ProviderUserAlreadyExists.kErrorCode:
87-
return ProviderUserAlreadyExists._(
87+
case _ProviderUserAlreadyExistsError.kErrorCode:
88+
return _ProviderUserAlreadyExistsError._(
8889
route: route,
8990
statusCode: statusCode,
9091
message: message,
@@ -171,7 +172,7 @@ class InvalidSessionError extends BackendRequestError {
171172
}
172173

173174
/// Thrown when the verification code is expired.
174-
class ExpiredCodeError extends BackendRequestError {
175+
class ExpiredCodeError extends BackendRequestError with UserError {
175176
/// The expired code error code.
176177
static const String kErrorCode = 'expiredCode';
177178

@@ -187,12 +188,12 @@ class ExpiredCodeError extends BackendRequestError {
187188
}
188189

189190
/// Thrown when the verification code is invalid.
190-
class InvalidVerificationCodeError extends BackendRequestError {
191+
class _InvalidVerificationCodeError extends BackendRequestError with UserError {
191192
/// The invalid verification code error code.
192193
static const String kErrorCode = 'invalidVerificationCode';
193194

194195
/// Creates a new invalid verification code error instance.
195-
InvalidVerificationCodeError._({
196+
_InvalidVerificationCodeError._({
196197
required super.route,
197198
required super.statusCode,
198199
super.message,
@@ -203,12 +204,12 @@ class InvalidVerificationCodeError extends BackendRequestError {
203204
}
204205

205206
/// Thrown when the provider authorization code is invalid.
206-
class InvalidAuthorizationCodeError extends BackendRequestError {
207+
class _InvalidAuthorizationCodeError extends BackendRequestError with UserError {
207208
/// The invalid authorization code error code.
208209
static const String kErrorCode = 'invalidAuthorizationCode';
209210

210211
/// Creates a new invalid authorization code error instance.
211-
InvalidAuthorizationCodeError._({
212+
_InvalidAuthorizationCodeError._({
212213
required super.route,
213214
required super.statusCode,
214215
super.message,
@@ -235,12 +236,12 @@ class InvalidAppVersionError extends BackendRequestError {
235236
}
236237

237238
/// Thrown when the provider user already exists.
238-
class ProviderUserAlreadyExists extends BackendRequestError {
239+
class _ProviderUserAlreadyExistsError extends BackendRequestError with UserError {
239240
/// The provider user already exists error code.
240241
static const String kErrorCode = 'providerUserAlreadyExists';
241242

242243
/// Creates a new expired session error instance.
243-
ProviderUserAlreadyExists._({
244+
_ProviderUserAlreadyExistsError._({
244245
required super.route,
245246
required super.statusCode,
246247
super.message,

lib/utils/result/reporter.dart

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import 'dart:async';
22
import 'dart:io';
33

44
import 'package:flutter/foundation.dart';
5-
import 'package:open_authenticator/model/backend/authentication/providers/provider.dart';
6-
import 'package:open_authenticator/model/backend/request/error.dart';
7-
import 'package:open_authenticator/model/backup/backup.dart';
85
import 'package:open_authenticator/utils/result/result.dart';
96
import 'package:open_authenticator/utils/sentry.dart';
107
import 'package:sentry_flutter/sentry_flutter.dart';
@@ -87,12 +84,7 @@ bool _sendToSentry(Object? ex) =>
8784
switch (ex) {
8885
SocketException(:final osError) => !{7, 8}.contains(osError?.errorCode),
8986
TimeoutException() => false,
90-
ProviderUserAlreadyExists() => false,
91-
ExpiredCodeError() => false,
92-
EmailAlreadySentException() => false,
93-
InvalidVerificationCodeError() => false,
94-
InvalidAuthorizationCodeError() => false,
95-
InvalidBackupPasswordException() => false,
87+
UserError(:final reportToSentry) => reportToSentry,
9688
_ => true,
9789
};
9890

@@ -144,3 +136,9 @@ class SentryResultReporter implements ResultReporter {
144136
);
145137
}
146138
}
139+
140+
/// An user error, that may not be reported to Sentry.
141+
mixin UserError {
142+
/// Whether the error should be reported to Sentry.
143+
bool get reportToSentry => false;
144+
}

0 commit comments

Comments
 (0)