exceptions 0.6.1 copy "exceptions: ^0.6.1" to clipboard
exceptions: ^0.6.1 copied to clipboard

This package assists in catching and handling exceptions that occur, producing a single result that contains either an error or a value.

example/example.dart

import 'dart:io';

import 'package:exceptions/exceptions.dart';
import 'package:http/http.dart';

extension ExceptionHandling on Client {
  Future<Result<Response>> getGuarded(Uri url, {Map<String, String>? headers}) {
    return Result.fromAsync(
      () => get(url, headers: headers).then((response) {
        if (response.statusCode != 200) {
          throw HttpException('${response.statusCode}');
        }
        return response;
      }),
      exceptionHandler: (exception, stackTrace) {
        switch (exception.runtimeType) {
          case SocketException:
            return ErrorMessage(
                source: 'http',
                message: 'No Internet connection',
                details: url);
          case HttpException:
            return ErrorMessage(
                source: 'http',
                message: 'Web request returned error',
                details: url);
          case FormatException:
            return ErrorMessage(
                source: 'http', message: 'Bad response format', details: url);
          default:
            return null;
        }
      },
    );
  }
}
1
likes
140
pub points
58%
popularity

Publisher

verified publisherfabi.online

This package assists in catching and handling exceptions that occur, producing a single result that contains either an error or a value.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on exceptions