Skip to content

Add validation for missing company product types on farmer import - #8

Open
candreszg wants to merge 1 commit into
agstack:mainfrom
candreszg:fix/farmer-import-500-error
Open

Add validation for missing company product types on farmer import#8
candreszg wants to merge 1 commit into
agstack:mainfrom
candreszg:fix/farmer-import-500-error

Conversation

@candreszg

Copy link
Copy Markdown

What does this PR do?

Adds a pre-condition check in UserCustomerImportService.importFarmersSpreadsheet() so that when a company has no product types configured (no value chains associated), the endpoint returns a clear INVALID_REQUEST error (HTTP
400) instead of crashing with IndexOutOfBoundsException (HTTP 500).

Root cause

readCompanyProductTypes() derives product types from the company's value chains. If none are associated, the
list is empty. The subsequent companyProductTypes.subList(0, 1) then throws IndexOutOfBoundsException: toIndex= 1, which the generic exception handler surfaces as an unhelpful "Uncaught exception java.lang.IndexOutOfBoundsException".

Fix

Added an early validation after readCompanyProductTypes() that throws ApiException(ApiStatus.INVALID_REQUEST, ...) with an actionable message, consistent with other pre-condition checks in the same class.

How to test

  1. Create a company with no value chains associated.
  2. Upload a valid .xlsx farmers spreadsheet and call the import endpoint.
  3. Before this PR: HTTP 500, "Uncaught exception java.lang.IndexOutOfBoundsException".
  4. After this PR: HTTP 400, "Company has no product types configured. Please associate the company with a value chain before importing farmers.".
  5. Associate the company with a value chain and retry: import works normally.

Closes #7

When a company has no value chains associated, readCompanyProductTypes returns an empty list, causing subList(0, 1) to throw IndexOutOfBoundsException (HTTP 500). Now an ApiException (HTTP 400) with a clear message is thrown instead, instructing the user to associate the company with a value chain first.

Signed-off-by: Camilo <71578145+candreszg@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Farmer import crashes with 500 when company has no value chains configured

1 participant