Tienisto / rhttp

Make HTTP requests using Rust from Flutter.
https://pub.dev/packages/rhttp
MIT License
69 stars 6 forks source link

Unable to catch RhttpError when using HTTP client #8

Closed kodjodevf closed 4 weeks ago

kodjodevf commented 4 weeks ago

Here is an example of when there is no internet connection it should throw an exception that I could display but I only see it in the console:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:rhttp/rhttp.dart';

Future<void> main() async {
  await Rhttp.init();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  http.Client? _client;
  http.Response? response;
  String? errorMessage;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Test Page'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: () async {
                  setState(() {
                    response = null;
                    errorMessage = null;
                  });
                  try {
                    _client ??= await RhttpCompatibleClient.create();

                    final res = await _client!.get(
                      Uri.https('reqres.in', '/api/users', {'page': '5'}),
                    );
                    setState(() {
                      response = res;
                    });
                  } catch (e) {
                    setState(() {
                      errorMessage = e.toString();
                    });
                  }
                },
                child: const Text('Test'),
              ),
              if (errorMessage != null) Text(errorMessage!),
              if (response != null) Text(response!.statusCode.toString()),
              if (response != null)
                Card(
                  child: Text(response!.body.substring(0, 100).toString()),
                ),
              if (response != null)
                Card(
                  child: Text(response!.headers.toString()),
                ),
            ],
          ),
        ),
      ),
    );
  }
}

and also the same when I try to access other urls which normally should return me the status different from 200.

Tienisto commented 4 weeks ago

I've added RhttpConnectionException in v0.5.3

kodjodevf commented 4 weeks ago

Works well thanks

Tienisto commented 4 weeks ago

In v0.5.4, every exception is now wrapped as RhttpWrappedClientException so you can access the original RhttpException for more type safety