handleError method

  1. @override
ErrorDetail handleError(
  1. String? errorId,
  2. ErrorDetail? error
)

Returns the ErrorDetail after manipulating an existing one.

Method that handles errors by parsing the raw response or custom input.

If other errors are to be added, then they will be handled here as if-else or switch-case.

Each error needs to be supplied an errorId, or null if a Generic Error were to be returned.

If error is passed, it can be manipulated within this method and return the modified ErrorDetail object.

Implementation

@override
ErrorDetail handleError(String? errorId, ErrorDetail? error) {
  error ??= RenovationController.genericError(error);

  ErrorDetail err;
  switch (errorId) {
    case 'get_doc_count':
      bool containsMissingTable = error.info?.rawError?.response?.data
              ?.contains('TableMissingError') ??
          false;
      if (error.info?.httpCode == 404 || containsMissingTable) {
        err = handleError('doctype_not_exist', error);
      } else {
        err = handleError(null, error);
      }
      break;
    case 'get_doc_meta':
      if (error.info?.httpCode == 404) {
        err = handleError('doctype_not_exist', error);
      } else {
        err = handleError(null, error);
      }
      break;
    case 'get_doc_info':
      if (identical(error.info?.httpCode, 404)) {
        err = handleError('docname_not_exist', error);
      } else if (identical(error.info?.httpCode, 500)) {
        err = handleError('doctype_not_exist', error);
      } else if (identical(error.title, 'DocInfo Not Found')) {
        error.type = RenovationError.NotFoundError;
        error.info = Information(
            data: error.info, httpCode: 404, cause: 'Docinfo not found');
        err = ErrorDetail(
            title: error.title, type: error.type, info: error.info);
      } else {
        err = handleError(null, error);
      }
      break;
    case 'doctype_not_exist':
      error
        ..title = FrappeModelController.DOCTYPE_NOT_EXIST_TITLE
        ..type = RenovationError.NotFoundError
        ..info = (error.info = Information(
            data: error.info,
            httpCode: 404,
            cause: FrappeModelController.DOCTYPE_NOT_EXIST_TITLE,
            suggestion:
                'Make sure the queried doctype is correct or create the required doctype'));
      err = ErrorDetail(title: error.title, info: error.info);
      break;
    case 'docname_not_exist':
      error
        ..title = FrappeModelController.DOCNAME_NOT_EXIST_TITLE
        ..type = RenovationError.NotFoundError
        ..info = ((error.info ?? Information())
          ..httpCode = 404
          ..cause = FrappeModelController.DOCNAME_NOT_EXIST_TITLE
          ..suggestion =
              'Make sure the queried document name is correct or create the required document');
      err = ErrorDetail(title: error.title, info: error.info);
      break;
    default:
      err = RenovationController.genericError(error);
  }
  return err;
}