Skip to content

POST /Booking returns 500 Internal Server Error instead of helpful 400 respons #1547

Open

Description

@BenMoeckel

When API clients attempt to create Booking or BookingSupplement entities directly via POST /processor/Booking, they receive a 500 Internal Server Error that doesn't explain what went wrong or how to fix it.

Steps to Reproduce

curl -X POST "http://localhost:4004/processor/Booking" \
  -u "amy:" \
  -H "Content-Type: application/json" \
  -d '{
    "BookingUUID": "01234567-89ab-cdef-0123-456789abcdef",
    "BookingDate": "2017-04-13",
    "ConnectionID": "",
    "FlightDate": "2017-04-13",
    "FlightPrice": 0,
    "CurrencyCode_code": "",
    "BookingStatus_code": "",
    "to_BookSupplement": [],
    "to_Carrier_AirlineID": "",
    "to_Customer_CustomerID": "",
    "to_Travel_TravelUUID": "01234567-89ab-cdef-0123-456789abcdef"
  }'

Expected Behavior

A 400 Bad Request with a helpful message explaining that Booking entities must be created via their parent Travel entity:

{
  "code": "400",
  "message": "Bookings must be created in the context of a Travel draft. Use POST /processor/Travel(TravelUUID=<uuid>,IsActiveEntity=false)/to_Booking instead."
}

Actual Behavior

{
  "error": {
    "message": "Internal Server Error",
    "code": "500"
  }
}

Root Cause

  1. Booking and BookingSupplement are composition entities that can only be created via their parent (Travel → Booking → BookingSupplement).
  2. The error occurs at the database layer and bubbles up as a generic 500 Internal Server Error.
  3. No validation exists to catch this earlier and provide a helpful, actionable error message.

Proposed Solution

Add request validation handlers in srv/travel-service.ts:

  • A before handler to reject direct POST /Booking requests before they reach the database.
  • An on('error') handler as a fallback to catch any remaining database-level errors and convert them from 500 Internal Server Error to 400 Bad Request with actionable guidance.

Additional Issues Found

Potential null pointer crashes in existing handlers in srv/travel-service.ts:

  • update_totalsGreen() crashes when the Travel draft cannot be found.
  • deductDiscount() accesses travel.ID when travel is null.
  • Date validation is missing null checks before comparing dates.

Environment

  • @sap/cds: 10.x
  • Node.js: 22.x

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions