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 'create_folder':
      if (error.info?.httpCode == 412) {
        error = handleError('invalid_foldername_forward_slash', error);
      } else if (error.info?.httpCode == 409) {
        error
          ..title = 'Folder with same name exists'
          ..type = RenovationError.DuplicateEntryError
          ..info = ((error.info ?? Information())
            ..httpCode = 409
            ..cause = 'A folder with same name exists'
            ..suggestion =
                'Choose a new name for the folder or remove the existing one');
      } else {
        error = handleError(null, error);
      }
      break;
    case 'invalid_foldername_forward_slash':
      error
        ..title = 'Invalid Folder Name'
        ..info = ((error.info ?? Information())
          ..httpCode = 412
          ..cause = 'Invalid folder name: contains forward slash'
          ..suggestion = 'Remove the forward slash from the name');
      break;
    case 'uploadViaHttp':
      if (error.info?.rawResponse?.data?.contains(
              'Same file has already been attached to the record') ??
          false) {
        error
          ..type = RenovationError.DuplicateEntryError
          ..title = 'File already exists with attached to the document'
          ..info = ((error.info ?? Information())
            ..httpCode = 409
            ..cause = 'Same file was uploaded earlier to the document'
            ..suggestion =
                'Either delete the file before re-uploading or ignore the message');
      } else {
        error = handleError(null, error);
      }
      break;
    case 'checkFolderExists':
    default:
      error = RenovationController.genericError(error);
  }

  return error;
}