KScript docs
EN

Databases: KScript.Data

Errors

KScript.Data throws errors you can catch by type. In a route, each one becomes a sensible HTTP status for you:

  • ModelNotFound, a kind of NotFound, comes from ->find_or_fail() and ->first_or_fail(). $e.model and $e.id say what wasn't found. A route answers 404.
  • UniqueViolation, a kind of Conflict, means a save would give two records the same unique value. $e.fields names the fields. A route answers 409.
  • ForeignKeyViolation, also a Conflict, means a change would leave a row pointing at one that isn't there. A route answers 409.
  • Query means the database refused a query, or a condition can't be turned into SQL. A route answers 500.
  • MultipleRecordsFound, a kind of Query, comes from ->sole() when more than one row matches.
  • Connection means the database can't be opened. A route answers 503.
<try>
  <do>User->create(email: 'ada@example.com')</do>
  <catch type="UniqueViolation" as="e">
    <echo>That email is already taken.</echo>
  </catch>
</try>
try {
  User->create(email: 'ada@example.com')
} catch UniqueViolation as e {
  echo "That email is already taken."
}