HumanException.fromError constructor

HumanException.fromError({
  1. required String humanMessage,
  2. required Object? error,
})

Implementation

factory HumanException.fromError({
  required String humanMessage,
  required Object? error,
}) {
  String? message;
  StackTrace? stackTrace;

  if (error is Error) {
    message = error.toString();
    stackTrace = error.stackTrace;
  } else if (error is List<dynamic> && error.length == 2) {
    final Object? listError = error.first;
    final Object? listStackTrace = error.last;
    message = listError._extract((object) => object.message) ?? listError.toString();
    if (listStackTrace is StackTrace) {
      stackTrace = listStackTrace;
    }
  } else {
    message = error.toString();
    stackTrace = error._extract((object) => object.stack) ?? error._extract((object) => object.stackTrace);
  }
  message = message.replaceFirst('Exception: ', '');

  final List<HumanException> errorsStack = [];
  if (error is HumanException) {
    errorsStack.add(error);
  }

  return HumanException(
    humanMessage: humanMessage,
    originalMessage: message,
    stackTrace: stackTrace,
    errorsStack: errorsStack,
  );
}