Refactor document creation error handling to provide more informative error messages

This commit is contained in:
Leon Bösche
2026-01-14 04:22:13 +01:00
parent 94cb7bc99f
commit 5cb86d4fde

View File

@@ -1,3 +1,4 @@
import 'package:dio/dio.dart';
import 'dart:ui'; import 'dart:ui';
import 'dart:js_interop'; import 'dart:js_interop';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@@ -314,10 +315,13 @@ class _FileExplorerState extends State<FileExplorer> {
), ),
); );
try {
final fileService = getIt<FileService>(); final fileService = getIt<FileService>();
// If orgId is 'personal', treat as empty string for personal workspace // If orgId is 'personal', treat as empty string for personal workspace
final effectiveOrgId = (widget.orgId == 'personal') ? '' : widget.orgId; // Always treat 'personal' or empty orgId as personal workspace
final effectiveOrgId = (widget.orgId == 'personal' || widget.orgId.isEmpty)
? ''
: widget.orgId;
try {
final fileId = await fileService.createDocument( final fileId = await fileService.createDocument(
effectiveOrgId, effectiveOrgId,
currentPath, currentPath,
@@ -337,6 +341,16 @@ class _FileExplorerState extends State<FileExplorer> {
} }
} catch (e) { } catch (e) {
snackController.close(); snackController.close();
String errorMsg = e.toString();
// If DioError, try to extract backend error message
if (e is DioError) {
final data = e.response?.data;
if (data is Map && data.containsKey('message')) {
errorMsg = data['message'].toString();
} else if (data is String) {
errorMsg = data;
}
}
if (context.mounted) { if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
@@ -346,7 +360,7 @@ class _FileExplorerState extends State<FileExplorer> {
const SizedBox(width: 12), const SizedBox(width: 12),
Expanded( Expanded(
child: Text( child: Text(
'Failed to create document: $e', 'Failed to create document: $errorMsg',
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: const TextStyle(color: AppTheme.primaryText), style: const TextStyle(color: AppTheme.primaryText),
), ),