exception_templates 0.2.0-nullsafety copy "exception_templates: ^0.2.0-nullsafety" to clipboard
exception_templates: ^0.2.0-nullsafety copied to clipboard

outdated

Parameterized exception and error templates for Dart. Enables throwing and catching exception/error objects that are defined by their type argument.

Exception Templates #

Build Status

Introduction #

When handling a program exception, the two main concerns are in what context did it occur and what type of exception occured.

The library exception_templates provides parameterized classes that allow throwing and catching exceptions characterized by their type argument.

Using parameterized exceptions eliminates the need to define library or class specific exceptions and enables filtering caught exceptions based on their type argument.

To highlight the exception context use:

To emphasise the exception type use:

Usage #

To use this library include exception_templates as dependency in your pubspec.yaml file.

The program below demonstrates how to throw and catch objects of type ExceptionOf<T> where T is a generic type. Colour output can be globally enabled or disabled by setting the static field colorOutput to ColorOutput.ON or ColorOutput.OFF, respectively,


import 'package:exception_templates/exception_templates.dart';

// Defining error types:
class LengthMismatch extends ErrorType {}

// Defining exception types:
class EmptyUserInput extends ExceptionType {}

extension Subtraction on List<num> {
  /// Subtracts two numerical lists of same length.
  List<num> operator -(List<num> other) {
    if (length != other.length) {
      throw ErrorOfType<LengthMismatch>(
          message: 'Could not calculate: $this - $other.',
          invalidState: 'Length of $this does not match length of $other.',
          expectedState: 'Two operands with the same length.');
    }
    return List<num>.generate(length, (i) => this[i] - other[i]);
  }
}

void main(List<String> args) {
  final a = [1, 2];
  final b = [3, 4];
  final c = [...b, 5];

  // Catching an ErrorOfType<LenghtMismatch>.
  // Demo only! Errors should not be caught, while exceptions should be caught.
  try {
    print('b - a = ${b - a}');
    print('c - b = ${c - b}');
  } on ErrorOfType<LengthMismatch> catch (e) {
    print(e);
  }

  // Catching an ExceptionOfType<EmptyUserFeedback>.
  var userFeedback = '';
  try {
    throw ExceptionOfType<EmptyUserInput>(
      message: 'Could not process user feedback.',
      expectedState: 'A non-empty String.',
    );
  } on ExceptionOfType<EmptyUserInput> catch (e) {
    userFeedback = '${e.message} User did not leave a message.';
    print('Feedback: $userFeedback\n');
  }
}


A typical output produced when running the program above is shown below: Console Output

Examples #

A copy of the program shown in the section above can be found in the folder example.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

5
likes
0
pub points
52%
popularity

Publisher

verified publishersimphotonics.com

Parameterized exception and error templates for Dart. Enables throwing and catching exception/error objects that are defined by their type argument.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on exception_templates