diff --git a/lib/helpers/ipfs_link_attach_flow.dart b/lib/helpers/ipfs_link_attach_flow.dart index 99a3175a..a63c2534 100644 --- a/lib/helpers/ipfs_link_attach_flow.dart +++ b/lib/helpers/ipfs_link_attach_flow.dart @@ -1,7 +1,6 @@ import 'dart:convert'; import 'package:file_selector/file_selector.dart' as file_selector; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -42,30 +41,16 @@ Future 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; 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( settings: settings, bytes: bytes, @@ -75,6 +60,16 @@ Future attachIpfsLinkToDraft({ } on Exception catch (error) { showFailure(error); return; + } catch (error, stackTrace) { + FlutterError.reportError( + FlutterErrorDetails( + exception: error, + stack: stackTrace, + library: 'ipfs link attach', + ), + ); + showFailure(error); + return; } if (!context.mounted) return; diff --git a/lib/helpers/ipfs_upload_helper.dart b/lib/helpers/ipfs_upload_helper.dart index 44da8b16..3a1c95dd 100644 --- a/lib/helpers/ipfs_upload_helper.dart +++ b/lib/helpers/ipfs_upload_helper.dart @@ -5,6 +5,17 @@ import 'package:http/http.dart' as http; 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 { @visibleForTesting static http.Client? httpClient; @@ -55,14 +66,18 @@ class IpfsUploadHelper { }) async { final endpoint = uploadEndpoint.trim(); 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); if (endpointUri == null || !(endpointUri.isScheme('http') || endpointUri.isScheme('https')) || 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(); @@ -74,13 +89,17 @@ class IpfsUploadHelper { final isKubo = !isPinata && !isFilebase; final safeFileName = _sanitizeUploadFileName(fileName); 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) { - throw Exception('Filebase RPC requires an API token.'); + throw const IpfsUploadConfigException( + 'Filebase RPC requires an API token.', + ); } if (isKubo && !_kuboAddPath.hasMatch(endpointUri.path)) { - throw Exception( + throw const IpfsUploadConfigException( 'Custom (Kubo) upload endpoint must end with /api/v0/add.', ); }