mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-10 16:07:58 -06:00
Initial commit
This commit is contained in:
70
lib/core/logic/error_code_localization_mapper.dart
Normal file
70
lib/core/logic/error_code_localization_mapper.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:flutter_paperless_mobile/generated/l10n.dart';
|
||||
|
||||
String translateError(BuildContext context, ErrorCode code) {
|
||||
switch (code) {
|
||||
case ErrorCode.unknown:
|
||||
return S.of(context).errorMessageUnknonwnError;
|
||||
case ErrorCode.authenticationFailed:
|
||||
return S.of(context).errorMessageAuthenticationFailed;
|
||||
case ErrorCode.notAuthenticated:
|
||||
return S.of(context).errorMessageNotAuthenticated;
|
||||
case ErrorCode.documentUploadFailed:
|
||||
return S.of(context).errorMessageDocumentUploadFailed;
|
||||
case ErrorCode.documentUpdateFailed:
|
||||
return S.of(context).errorMessageDocumentUpdateFailed;
|
||||
case ErrorCode.documentLoadFailed:
|
||||
return S.of(context).errorMessageDocumentLoadFailed;
|
||||
case ErrorCode.documentDeleteFailed:
|
||||
return S.of(context).errorMessageDocumentDeleteFailed;
|
||||
case ErrorCode.documentPreviewFailed:
|
||||
return S.of(context).errorMessageDocumentPreviewFailed;
|
||||
case ErrorCode.documentAsnQueryFailed:
|
||||
return S.of(context).errorMessageDocumentAsnQueryFailed;
|
||||
case ErrorCode.tagCreateFailed:
|
||||
return S.of(context).errorMessageTagCreateFailed;
|
||||
case ErrorCode.tagLoadFailed:
|
||||
return S.of(context).errorMessageTagLoadFailed;
|
||||
case ErrorCode.documentTypeCreateFailed:
|
||||
return S.of(context).errorMessageDocumentTypeCreateFailed;
|
||||
case ErrorCode.documentTypeLoadFailed:
|
||||
return S.of(context).errorMessageDocumentTypeLoadFailed;
|
||||
case ErrorCode.correspondentCreateFailed:
|
||||
return S.of(context).errorMessageCorrespondentCreateFailed;
|
||||
case ErrorCode.correspondentLoadFailed:
|
||||
return S.of(context).errorMessageCorrespondentLoadFailed;
|
||||
case ErrorCode.scanRemoveFailed:
|
||||
return S.of(context).errorMessageScanRemoveFailed;
|
||||
case ErrorCode.invalidClientCertificateConfiguration:
|
||||
return S.of(context).errorMessageInvalidClientCertificateConfiguration;
|
||||
case ErrorCode.documentBulkDeleteFailed:
|
||||
return S.of(context).errorMessageBulkDeleteDocumentsFailed;
|
||||
case ErrorCode.biometricsNotSupported:
|
||||
return S.of(context).errorMessageBiotmetricsNotSupported;
|
||||
case ErrorCode.biometricAuthenticationFailed:
|
||||
return S.of(context).errorMessageBiometricAuthenticationFailed;
|
||||
case ErrorCode.deviceOffline:
|
||||
return S.of(context).errorMessageDeviceOffline;
|
||||
case ErrorCode.serverUnreachable:
|
||||
return S.of(context).errorMessageServerUnreachable;
|
||||
case ErrorCode.similarQueryError:
|
||||
return S.of(context).errorMessageSimilarQueryError;
|
||||
case ErrorCode.autocompleteQueryError:
|
||||
return S.of(context).errorMessageAutocompleteQueryError;
|
||||
case ErrorCode.storagePathLoadFailed:
|
||||
return S.of(context).errorMessageStoragePathLoadFailed;
|
||||
case ErrorCode.storagePathCreateFailed:
|
||||
return S.of(context).errorMessageStoragePathCreateFailed;
|
||||
case ErrorCode.loadSavedViewsError:
|
||||
return S.of(context).errorMessageLoadSavedViewsError;
|
||||
case ErrorCode.createSavedViewError:
|
||||
return S.of(context).errorMessageCreateSavedViewError;
|
||||
case ErrorCode.deleteSavedViewError:
|
||||
return S.of(context).errorMessageDeleteSavedViewError;
|
||||
case ErrorCode.requestTimedOut:
|
||||
return S.of(context).errorMessageRequestTimedOut;
|
||||
default:
|
||||
return S.of(context).errorMessageUnknonwnError;
|
||||
}
|
||||
}
|
||||
135
lib/core/logic/timeout_client.dart
Normal file
135
lib/core/logic/timeout_client.dart
Normal file
@@ -0,0 +1,135 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:flutter_paperless_mobile/core/type/json.dart';
|
||||
import 'package:flutter_paperless_mobile/di_initializer.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
///
|
||||
/// Convenience class which handles timeout errors.
|
||||
///
|
||||
@Injectable(as: BaseClient)
|
||||
@Named("timeoutClient")
|
||||
class TimeoutClient implements BaseClient {
|
||||
static const Duration requestTimeout = Duration(seconds: 25);
|
||||
|
||||
@override
|
||||
Future<StreamedResponse> send(BaseRequest request) async {
|
||||
return getIt<BaseClient>().send(request).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void close() {
|
||||
getIt<BaseClient>().close();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> delete(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>()
|
||||
.delete(url, headers: headers, body: body, encoding: encoding)
|
||||
.timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> get(Uri url, {Map<String, String>? headers}) async {
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().get(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> head(Uri url, {Map<String, String>? headers}) async {
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().head(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> patch(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>()
|
||||
.patch(url, headers: headers, body: body, encoding: encoding)
|
||||
.timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> post(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().post(url, headers: headers, body: body, encoding: encoding).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Response> put(Uri url,
|
||||
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
|
||||
return _handle400Error(
|
||||
await getIt<BaseClient>().put(url, headers: headers, body: body, encoding: encoding).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> read(Uri url, {Map<String, String>? headers}) async {
|
||||
return getIt<BaseClient>().read(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers}) {
|
||||
return getIt<BaseClient>().readBytes(url, headers: headers).timeout(
|
||||
requestTimeout,
|
||||
onTimeout: () => Future.error(const ErrorMessage(ErrorCode.requestTimedOut)),
|
||||
);
|
||||
}
|
||||
|
||||
Response _handle400Error(Response response) {
|
||||
if (response.statusCode == 400) {
|
||||
// try to parse contained error message, otherwise return response
|
||||
final JSON json = jsonDecode(response.body);
|
||||
final Map<String, String> errorMessages = {};
|
||||
//TODO: This could be simplified, look at error message format of paperless-ngx
|
||||
for (final entry in json.entries) {
|
||||
if (entry.value is List) {
|
||||
errorMessages.putIfAbsent(entry.key, () => (entry.value as List).cast<String>().first);
|
||||
} else if (entry.value is String) {
|
||||
errorMessages.putIfAbsent(entry.key, () => entry.value);
|
||||
} else {
|
||||
errorMessages.putIfAbsent(entry.key, () => entry.value.toString());
|
||||
}
|
||||
}
|
||||
throw errorMessages;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user