Envelope and errors
One JSON envelope for every response; branch on success, then on code.
Success
{ "success": true, "data": { }, "message": "optional", "meta": { } }
Lists put an array in data and pagination in meta.pagination. 204 responses (deletes) have no body.
Error
{
"success": false,
"message": "Validation failed",
"code": "VALIDATION_ERROR",
"errors": { "code": ["The code field is required."] }
}
code |
HTTP | PHP exception |
|---|---|---|
VALIDATION_ERROR |
422 | LedgaValidationException |
UNAUTHORIZED |
401 | LedgaAuthenticationException |
FORBIDDEN |
403 | LedgaAuthorizationException |
NOT_FOUND |
404 | LedgaNotFoundException |
CONFLICT |
409 | LedgaConflictException |
RATE_LIMIT_EXCEEDED |
429 | LedgaRateLimitException |
SERVER_ERROR |
5xx | LedgaServerException |
errors appears only on 422, keyed by field. meta carries retry_after on 429 and error_code on 409. Some 403s use a more specific code such as ATTRIBUTE_ADMIN_CAPABILITY_REQUIRED.
use Ledga\Api\Exceptions\LedgaException;
use Ledga\Api\Exceptions\LedgaValidationException;
try {
$ledga->accounts->create(['code' => '1000', 'name' => 'Cash', 'type' => 'asset', 'category' => 'system']);
} catch (LedgaValidationException $e) {
$e->getErrors(); // ['code' => ['The code field is required.']]
} catch (LedgaException $e) {
$e->getMessage();
}