Show IPFS upload configuration errors instead of failing silently
Throw IpfsUploadConfigException for upload configuration problems, and route every failure in the attach flow to the user, so a missing key or bad endpoint no longer escapes as an unhandled ArgumentError.
This commit is contained in:
1 parent
cc33595f13
commit
084a8fb98b
2 files changed
+42
-28
No files matched your search
@@ -1,7 +1,6 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:file_selector/file_selector.dart' as file_selector;
|
import 'package:file_selector/file_selector.dart' as file_selector;
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
@@ -42,30 +41,16 @@ Future<void> attachIpfsLinkToDraft({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final file_selector.XFile? file;
|
|
||||||
try {
|
|
||||||
file = await file_selector.openFile();
|
|
||||||
} on Exception catch (error) {
|
|
||||||
showFailure(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (file == null || !context.mounted) return;
|
|
||||||
|
|
||||||
final Uint8List bytes;
|
|
||||||
try {
|
|
||||||
bytes = await file.readAsBytes();
|
|
||||||
} on Exception catch (error) {
|
|
||||||
showFailure(error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!context.mounted) return;
|
|
||||||
if (bytes.isEmpty) {
|
|
||||||
showFailure('The selected file is empty.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final String messageUrl;
|
final String messageUrl;
|
||||||
try {
|
try {
|
||||||
|
final file = await file_selector.openFile();
|
||||||
|
if (file == null || !context.mounted) return;
|
||||||
|
final bytes = await file.readAsBytes();
|
||||||
|
if (!context.mounted) return;
|
||||||
|
if (bytes.isEmpty) {
|
||||||
|
showFailure('The selected file is empty.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
messageUrl = await IpfsUploadHelper.uploadImageWithSettings(
|
messageUrl = await IpfsUploadHelper.uploadImageWithSettings(
|
||||||
settings: settings,
|
settings: settings,
|
||||||
bytes: bytes,
|
bytes: bytes,
|
||||||
@@ -75,6 +60,16 @@ Future<void> attachIpfsLinkToDraft({
|
|||||||
} on Exception catch (error) {
|
} on Exception catch (error) {
|
||||||
showFailure(error);
|
showFailure(error);
|
||||||
return;
|
return;
|
||||||
|
} catch (error, stackTrace) {
|
||||||
|
FlutterError.reportError(
|
||||||
|
FlutterErrorDetails(
|
||||||
|
exception: error,
|
||||||
|
stack: stackTrace,
|
||||||
|
library: 'ipfs link attach',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
showFailure(error);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (!context.mounted) return;
|
if (!context.mounted) return;
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,17 @@ import 'package:http/http.dart' as http;
|
|||||||
|
|
||||||
import '../models/app_settings.dart';
|
import '../models/app_settings.dart';
|
||||||
|
|
||||||
|
/// Thrown when the IPFS upload settings can't be used, before or instead of
|
||||||
|
/// a network failure.
|
||||||
|
class IpfsUploadConfigException implements Exception {
|
||||||
|
const IpfsUploadConfigException(this.message);
|
||||||
|
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => message;
|
||||||
|
}
|
||||||
|
|
||||||
class IpfsUploadHelper {
|
class IpfsUploadHelper {
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
static http.Client? httpClient;
|
static http.Client? httpClient;
|
||||||
@@ -55,14 +66,18 @@ class IpfsUploadHelper {
|
|||||||
}) async {
|
}) async {
|
||||||
final endpoint = uploadEndpoint.trim();
|
final endpoint = uploadEndpoint.trim();
|
||||||
if (endpoint.isEmpty) {
|
if (endpoint.isEmpty) {
|
||||||
throw Exception('Configure an IPFS upload endpoint first.');
|
throw const IpfsUploadConfigException(
|
||||||
|
'Configure an IPFS upload endpoint first.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final endpointUri = Uri.tryParse(endpoint);
|
final endpointUri = Uri.tryParse(endpoint);
|
||||||
if (endpointUri == null ||
|
if (endpointUri == null ||
|
||||||
!(endpointUri.isScheme('http') || endpointUri.isScheme('https')) ||
|
!(endpointUri.isScheme('http') || endpointUri.isScheme('https')) ||
|
||||||
endpointUri.host.isEmpty) {
|
endpointUri.host.isEmpty) {
|
||||||
throw Exception('The IPFS upload endpoint must be an http(s) URL.');
|
throw const IpfsUploadConfigException(
|
||||||
|
'The IPFS upload endpoint must be an http(s) URL.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final token = uploadApiKey.trim();
|
final token = uploadApiKey.trim();
|
||||||
@@ -74,13 +89,17 @@ class IpfsUploadHelper {
|
|||||||
final isKubo = !isPinata && !isFilebase;
|
final isKubo = !isPinata && !isFilebase;
|
||||||
final safeFileName = _sanitizeUploadFileName(fileName);
|
final safeFileName = _sanitizeUploadFileName(fileName);
|
||||||
if (isPinata && token.isEmpty) {
|
if (isPinata && token.isEmpty) {
|
||||||
throw Exception('Pinata requires an API key or JWT.');
|
throw const IpfsUploadConfigException(
|
||||||
|
'Pinata requires an API key or JWT.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (isFilebase && token.isEmpty) {
|
if (isFilebase && token.isEmpty) {
|
||||||
throw Exception('Filebase RPC requires an API token.');
|
throw const IpfsUploadConfigException(
|
||||||
|
'Filebase RPC requires an API token.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (isKubo && !_kuboAddPath.hasMatch(endpointUri.path)) {
|
if (isKubo && !_kuboAddPath.hasMatch(endpointUri.path)) {
|
||||||
throw Exception(
|
throw const IpfsUploadConfigException(
|
||||||
'Custom (Kubo) upload endpoint must end with /api/v0/add.',
|
'Custom (Kubo) upload endpoint must end with /api/v0/add.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user