This repository has been archived on 2026-06-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FabledApp/lib/screens/setup/setup_screen.dart
T
bvandeusen abf91874c3 Show specific error reason on setup screen connection failure
Instead of a generic "Could not reach server" for all failures, show
the actual cause: SSL cert error, timeout, or the raw connection error
message. This makes it possible to diagnose setup issues on-device
without needing a debugger.

Also extend connect timeout from 5s to 10s.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-01 14:48:49 -05:00

138 lines
4.5 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../core/constants.dart';
import '../../providers/settings_provider.dart';
class SetupScreen extends ConsumerStatefulWidget {
const SetupScreen({super.key});
@override
ConsumerState<SetupScreen> createState() => _SetupScreenState();
}
class _SetupScreenState extends ConsumerState<SetupScreen> {
final _formKey = GlobalKey<FormState>();
final _urlController = TextEditingController();
bool _testing = false;
String? _error;
@override
void dispose() {
_urlController.dispose();
super.dispose();
}
Future<void> _testAndSave() async {
if (!_formKey.currentState!.validate()) return;
setState(() {
_testing = true;
_error = null;
});
var url = _urlController.text.trim();
if (url.endsWith('/')) url = url.substring(0, url.length - 1);
try {
final dio = Dio(BaseOptions(
connectTimeout: const Duration(seconds: 10),
// Accept any HTTP status — only network-level failures throw.
// A 401 or 200 both mean the server is reachable.
validateStatus: (_) => true,
));
await dio.get('$url/api/auth/status');
} on DioException catch (e) {
final msg = switch (e.type) {
DioExceptionType.badCertificate =>
'SSL certificate error. If using a self-signed cert, it must be trusted on this device.',
DioExceptionType.connectionTimeout ||
DioExceptionType.receiveTimeout =>
'Connection timed out. The server may be down or unreachable.',
DioExceptionType.connectionError =>
'Cannot connect: ${e.message ?? "network error"}',
_ => 'Error: ${e.message ?? e.type.name}',
};
setState(() {
_testing = false;
_error = msg;
});
return;
} catch (e) {
setState(() {
_testing = false;
_error = 'Unexpected error: $e';
});
return;
}
await ref.read(serverUrlProvider.notifier).setUrl(url);
if (mounted) context.go(Routes.login);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Text(
'Welcome to Fabled',
style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
const Text(
'Enter your FabledAssistant server URL to get started.',
textAlign: TextAlign.center,
),
const SizedBox(height: 32),
TextFormField(
controller: _urlController,
decoration: const InputDecoration(
labelText: 'Server URL',
hintText: 'https://fabled.example.com',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.url,
autocorrect: false,
validator: (v) {
if (v == null || v.trim().isEmpty) return 'Required';
final uri = Uri.tryParse(v.trim());
if (uri == null || !uri.hasScheme) {
return 'Enter a valid URL (include http:// or https://)';
}
return null;
},
),
if (_error != null) ...[
const SizedBox(height: 8),
Text(_error!, style: TextStyle(color: Theme.of(context).colorScheme.error)),
],
const SizedBox(height: 24),
FilledButton(
onPressed: _testing ? null : _testAndSave,
child: _testing
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Connect'),
),
],
),
),
),
),
);
}
}