Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RequiredFieldException for better handling of missing field errors #895

Merged
merged 1 commit into from
Jun 18, 2023

Conversation

oatmael
Copy link

@oatmael oatmael commented Jun 16, 2023

Adds an exception to allow for easier handling of missing field errors. Exposing the class & field on the exception lets users build their own error messages, which in turn allows for more human error messages to end users.

While it is currently possible to achieve this with string manipulation on the existing messages, having a distinct exception allows for much greater flexibility:

private const XERO_MISSING_FIELD_TO_HUMAN = [
  Invoice::class => [ 
   '$Contact' => 'Xero Contact',
   '$LineItems' => 'Line Items',
   ...
  ],
  ...
];

...
try {
   $this->uploadInvoice($data);
} catch (RequiredFieldException $e) {
   $human_field = self::XERO_MISSING_FIELD_TO_HUMAN[$e->getClass()][$e->getField()] ?? 'Unknown Field';
   $message = sprintf('%s is a required field, can not upload invoice.', $human_field);
   $this->setUserFlash($message);
} catch (BadRequestException $e) {
   ...
}

@calcinai calcinai merged commit e22b5cc into calcinai:master Jun 18, 2023
@calcinai
Copy link
Owner

LGTM, thanks

@oatmael
Copy link
Author

oatmael commented Nov 1, 2023

@calcinai are there any plans to tag in the near future? I can work around this but would prefer to have canonical tags where possible

@calcinai
Copy link
Owner

calcinai commented Nov 1, 2023

Shivers sorry, will do now!

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.

2 participants