Skip to content

Error Messages

All error responses follow the standard envelope with a structured errors object. Error codes use the format MIDDAG-{DOMAIN}-{CODE} for domain-specific errors and standard API codes for transport-level errors.

API Error Codes

These codes appear in the errors.code field of any error response:

CodeHTTPWhenExample Detail
VALIDATION_ERROR422Invalid input fields"Email is required"
AUTHENTICATION_ERROR401Missing, expired, or invalid token"Token expired"
AUTHORIZATION_ERROR403Insufficient permissions"Scope 'finances' required"
NOT_FOUND404Resource does not exist"Organization 42 not found"
CONFLICT409Conflicting state"Entitlement already cancelled"
RATE_LIMIT429Rate limit exceeded"5 requests/min exceeded"
INTERNAL_ERROR500Unexpected server error"Unexpected error"

Error Response Structure

json
{
    "success": false,
    "data": null,
    "meta": null,
    "message": "Validation failed",
    "errors": {
        "code": "VALIDATION_ERROR",
        "fields": {
            "email": [
                "Email is required"
            ],
            "name": [
                "Name must be at least 2 characters"
            ]
        }
    }
}

For non-validation errors, the errors object uses detail instead of fields:

json
{
    "success": false,
    "data": null,
    "meta": null,
    "message": "Authentication failed",
    "errors": {
        "code": "AUTHENTICATION_ERROR",
        "detail": "Token expired"
    }
}

Domain-Specific Error Codes

Organization Errors

CodeHTTPDescription
MIDDAG-ORG-NOT_FOUND404Organization does not exist
MIDDAG-ORG-CNPJ_INVALID422CNPJ failed federal API validation
MIDDAG-ORG-CNPJ_DUPLICATE409CNPJ already registered to another org
MIDDAG-ORG-DELETE_DENIED403Only the owner can delete an organization

Collaborator Errors

CodeHTTPDescription
MIDDAG-COLLAB-NOT_FOUND404Collaborator record not found
MIDDAG-COLLAB-INVITE_EXPIRED409Invite token has expired
MIDDAG-COLLAB-INVITE_USED409Invite already accepted or rejected
MIDDAG-COLLAB-ALREADY_MEMBER409User is already a collaborator
MIDDAG-COLLAB-ROLE_ESCALATION403Cannot assign a role higher than your own

Entitlement Errors

CodeHTTPDescription
MIDDAG-ENT-NOT_FOUND404Entitlement does not exist
MIDDAG-ENT-ALREADY_CANCELLED409Entitlement is already cancelled
MIDDAG-ENT-INVALID_TRANSITION422Status transition is not allowed
MIDDAG-ENT-INVALID_CLASS422Entitlement class is not recognized

Quote Errors

CodeHTTPDescription
MIDDAG-QUOTE-NOT_FOUND404Quote does not exist
MIDDAG-QUOTE-ALREADY_ACCEPTED409Quote was already accepted
MIDDAG-QUOTE-EXPIRED409Quote validity period has passed
MIDDAG-QUOTE-INVALID_TRANSITION422Status transition is not allowed

License Errors

CodeHTTPDescription
MIDDAG-LIC-NOT_FOUND404License does not exist
MIDDAG-LIC-UNAVAILABLE503License system is not available
MIDDAG-LIC-ACTIVATION_LIMIT409Maximum activations reached
MIDDAG-LIC-DOMAIN_REQUIRED422Domain parameter is required
MIDDAG-LIC-DOMAIN_NOT_ACTIVE409Domain is not currently activated

Service Errors

CodeHTTPDescription
MIDDAG-SVC-NOT_FOUND404Service does not exist
MIDDAG-SVC-INVALID_TRANSITION422Status transition is not allowed

Ticket Errors

CodeHTTPDescription
MIDDAG-SR-NOT_FOUND404Service request does not exist
MIDDAG-SR-INVALID_TRANSITION422Status transition is not allowed
MIDDAG-SR-ENV_ONLY403Clients can only create SRs for ENV entitlements

Authentication Errors

CodeHTTPDescription
MIDDAG-AUTH-INVALID_CREDS401Invalid email or password
MIDDAG-AUTH-TOKEN_EXPIRED401JWT has expired
MIDDAG-AUTH-TOKEN_REVOKED401Token revoked by logout
MIDDAG-AUTH-EMAIL_UNVERIFIED403Email not yet verified
MIDDAG-AUTH-ACCOUNT_DISABLED403Account has been disabled
MIDDAG-AUTH-RATE_LIMITED429Too many authentication attempts

Handling Errors in Client Code

typescript
const response = await fetch('/wp-json/middag-account/v1/entitlements', {
  headers: {
    'Authorization': `Bearer ${token}`,
    'X-Middag-Organization': orgId,
  },
});

const body = await response.json();

if (!body.success) {
  if (body.errors.code === 'AUTHENTICATION_ERROR') {
    // Redirect to login or refresh token
  } else if (body.errors.code === 'VALIDATION_ERROR') {
    // Show field-level errors from body.errors.fields
  }
}