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);
  switch (errorId) {
    case 'get_doc':
      if (error.info?.httpCode == 404) {
        error = handleError('non_existing_doc', error);
      } else if (error.info?.data != null &&
          error.info?.data?.exception != null) {
        if (error.info?.data.exception.contains('DoesNotExistError')) {
          error = handleError('non_existing_doc', error);
        } else if (error.info?.data.exception.contains('ImportError')) {
          error = handleError('non_existing_doctype', error);
        }
      } else if (error.info?.httpCode == 412) {
        error = handleError('wrong_input', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'wrong_input':
      error
        ..type = RenovationError.DataFormatError
        ..title = 'Wrong input'
        ..info = ((error.info ?? Information())
          ..httpCode = 412
          ..cause = 'The input arguments are in the wrong type/format'
          ..suggestion =
              'Use the correct parameters types/formats referencing the functions signature'
          ..data = error.info);
      break;
    case 'delete_doc':
      if (error.info?.httpCode == 404) {
        error = handleError('non_existing', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'get_report':
      if (error.info?.httpCode == 404 &&
          error.info?.data != null &&
          error.info?.data.excType.contains('DoesNotExistError')) {
        error = handleError('non_existing_doctype', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'set_value':
    case 'add_tag':
    case 'remove_tag':
      if (error.info?.httpCode == 500) {
        error = handleError('non_existing_doctype', error);
      } else if (error.info?.httpCode == 404) {
        error = handleError('non_existing_doc', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'get_value':
      if (error.info?.httpCode == 200) {
        error = handleError('non_existing', error);
      } else if (error.info?.httpCode == 500) {
        error = handleError('non_existing_docfield', error);
      } else {
        handleError(null, error);
      }
      break;
    case 'save_doc':
      if (error.info?.data != null &&
          error.info?.data.exception != null &&
          error.info?.data.exception.contains('DuplicateEntryError')) {
        error = handleError('duplicate_document', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'submit_doc':
    case 'save_submit_doc':
    case 'cancel_doc':
      if (error.info?.httpCode == 409) {
        error = handleError('duplicate_document', error);
      } else if (error.info?.httpCode == 500) {
        error = handleError('non_existing_doctype', error);
      } else if (error.info?.httpCode == 404) {
        error = handleError('non_existing_doc', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'search_link':
      if (error.info?.httpCode == 404 &&
          error.info?.data != null &&
          error.info?.data.excType == 'DoesNotExistError') {
        error = handleError('non_existing_doctype', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'get_tagged_docs':
      if (error.info?.httpCode == 500 &&
          error.info?.data != null &&
          error.info?.data.exc.contains("doesn't exist")) {
        error = handleError('non_existing_doctype', error);
      } else {
        error = handleError(null, error);
      }
      break;

    case 'get_tags':
      if (error.info?.httpCode == 500) {
        error = handleError('non_existing_doctype', error);
      } else {
        error = handleError(null, error);
      }
      break;
    case 'non_existing':
      error
        ..type = RenovationError.NotFoundError
        ..title = DOCTYPE_OR_DOCNAME_NOT_EXIST_TITLE
        ..info = ((error.info ?? Information())
          ..httpCode = 404
          ..cause = 'Doctype or Docname does not exist'
          ..suggestion = 'Make sure the queried document name is correct'
          ..data = error.info);
      break;
    case 'non_existing_doc':
      error
        ..type = RenovationError.NotFoundError
        ..title = DOCNAME_NOT_EXIST_TITLE
        ..info = ((error.info ?? Information())
          ..httpCode = 404
          ..cause = 'Docname does not exist'
          ..suggestion =
              'Make sure the queried document name is correct or create the required document'
          ..data = error.info);
      break;
    case 'non_existing_doctype':
      error
        ..type = RenovationError.NotFoundError
        ..title = DOCTYPE_NOT_EXIST_TITLE
        ..info = ((error.info ?? Information())
          ..httpCode = 404
          ..cause = 'DocType does not exist'
          ..suggestion =
              'Make sure the queried DocType is input correctly or create the required DocType'
          ..data = error.info);
      break;
    case 'non_existing_docfield':
      error
        ..title = 'DocField is not valid'
        ..type = RenovationError.NotFoundError
        ..info = ((error.info ?? Information())
          ..httpCode = 404
          ..cause = 'DocField is not defined for the DocType'
          ..suggestion =
              'Make sure the docfield is input. It is case-sensitive'
          ..data = error.info);
      break;
    case 'duplicate_document':
      error
        ..title = 'Duplicate document found'
        ..type = RenovationError.DuplicateEntryError
        ..info = ((error.info ?? Information())
          ..httpCode = 409
          ..cause = 'Duplicate doc found'
          ..suggestion =
              'Change the "name" field or delete the existing document'
          ..data = error.info);
      break;
    case 'get_list':
      if (error.info?.httpCode == 500 &&
          error.info?.data != null &&
          error.info?.data.exc.contains('TableMissingError')) {
        error = handleError('non_existing_doctype', error);
      }
      break;
    case 'assign_doc':
    case 'complete_doc_assignment':
    case 'unassign_doc':
    case 'get_docs_assigned_to_user':
    case 'get_users_assigned_to_doc':
    default:
      error = RenovationController.genericError(error);
  }
  return error;
}