tryDecode method

dynamic tryDecode(
  1. Configuration configuration,
  2. String name,
  3. dynamic decode()
)

Implementation

dynamic tryDecode(
  Configuration configuration,
  String name,
  dynamic Function() decode,
) {
  try {
    return decode();
  } on ConfigurationException catch (e) {
    throw ConfigurationException(
      configuration,
      e.message,
      keyPath: [name, ...e.keyPath],
    );
  } on IntermediateException catch (e) {
    final underlying = e.underlying;
    if (underlying is ConfigurationException) {
      final keyPaths = [
        [name],
        e.keyPath,
        underlying.keyPath,
      ].expand((i) => i).toList();

      throw ConfigurationException(
        configuration,
        underlying.message,
        keyPath: keyPaths,
      );
    } else if (underlying is TypeError) {
      throw ConfigurationException(
        configuration,
        "input is wrong type",
        keyPath: [name, ...e.keyPath],
      );
    }

    throw ConfigurationException(
      configuration,
      underlying.toString(),
      keyPath: [name, ...e.keyPath],
    );
  } catch (e) {
    throw ConfigurationException(
      configuration,
      e.toString(),
      keyPath: [name],
    );
  }
}