The PROFFIX REST API Wrapper provides enhanced error handling that captures both general error messages and field-level validation errors from the PROFFIX API.
When the PROFFIX API returns validation errors, they typically include:
- A general
Messagedescribing the overall error - A
Fieldsarray containing specific field validation errors
{
"Fields": [
{
"Reason": "EMPTY",
"Name": "PLZ",
"Message": "PLZ darf nicht leer bleiben!"
},
{
"Reason": "EMPTY",
"Name": "Land",
"Message": "Land darf nicht leer bleiben!"
}
],
"Message": "Mindestens ein Feld ist ungültig."
}The HttpClientException class now provides methods to access detailed error information:
use Pitwch\RestAPIWrapperProffix\HttpClient\HttpClientException;
try {
$response = $client->request('endpoint', 'POST', $data);
} catch (HttpClientException $e) {
// Get the main error message
echo $e->getMessage(); // "Mindestens ein Feld ist ungültig."
// Get HTTP status code
echo $e->getCode(); // e.g., 400
}try {
$response = $client->request('endpoint', 'POST', $data);
} catch (HttpClientException $e) {
// Check if field errors exist
if ($e->hasFieldErrors()) {
// Get array of field errors
$fieldErrors = $e->getFieldErrors();
foreach ($fieldErrors as $error) {
echo "Field: " . $error['Name'] . "\n";
echo "Message: " . $error['Message'] . "\n";
echo "Reason: " . $error['Reason'] . "\n";
}
}
}try {
$response = $client->request('endpoint', 'POST', $data);
} catch (HttpClientException $e) {
// Get a formatted message with all field errors
echo $e->getDetailedMessage();
/* Output:
* Mindestens ein Feld ist ungültig.
* Field errors:
* - PLZ: PLZ darf nicht leer bleiben! (Reason: EMPTY)
* - Land: Land darf nicht leer bleiben! (Reason: EMPTY)
*/
}Returns true if field-level validation errors are present.
Returns an array of field errors, where each error contains:
Name: The field nameMessage: The validation error messageReason: The reason code (e.g., "EMPTY", "INVALID", etc.)
Returns null if no field errors exist.
Returns a formatted string containing the main error message plus all field-level errors.
Returns only the main error message (standard Exception method).
Returns the HTTP status code (standard Exception method).
Returns the Request object that caused the error.
Returns the Response object if available.
- Always catch HttpClientException when making API requests
- Check for field errors using
hasFieldErrors()before accessing them - Use
getDetailedMessage()for logging to capture complete error context - Use
getFieldErrors()when you need to process individual field errors programmatically - Display user-friendly messages by mapping field names and error reasons to localized text
try {
$response = $client->request('Adresse', 'POST', $addressData);
} catch (HttpClientException $e) {
if ($e->hasFieldErrors()) {
$errors = [];
foreach ($e->getFieldErrors() as $error) {
$errors[$error['Name']] = $error['Message'];
}
// Display errors next to form fields
return [
'success' => false,
'message' => $e->getMessage(),
'field_errors' => $errors
];
}
// Generic error without field details
return [
'success' => false,
'message' => $e->getMessage()
];
}