abort()

Terminate the current transaction.

Signature

abort(notification: Any): Never

Description

The abort() method terminates the current transaction, which includes all expressions that are part of the query. Changes to data or schema by the aborted transaction are ignored even if the changes occurred before the abort() call.

Parameters

Parameter Type Required Description

notification

Any

Yes

User-defined notification or action associated with the abort request, which can be anything, such as a message, Struct, or Document.

Return value

Type Description

Never

The method returns the result of notification.

Examples

This simple example shows how to abort a transaction.

Create a document in the collection and abort the transaction:

Customer.create({ lastName: 'Jones' })
abort("Discard")
abort

error: Query aborted.
at *query*:2:6
  |
2 | abort("Discard")
  |      ^^^^^^^^^^^
  |

 

Run Customer.all() to verify that a customer with the last name of Jones wasn’t added.

The following example creates an anonymous function, which aborts when called without a valid email argument:

let createUser = (name, email) => {
  if (email.length <= 0) {
    abort({
      code: "invalid_email",
      message: "user email must be non-empty"
    })
  }
  Users.create({
    name: name,
    email: email
  })
}

createUser("name", "email")
createUser("name", "")
Query aborted.

error: Query aborted.
at *query*:3:10
  |
3 |       abort({
  |  __________^
4 | |       code: "invalid_email",
5 | |       message: "user email must be non-empty"
6 | |     })
  | |______^
  |
at *query*:15:11
   |
15 | createUser("name", "")
   |           ^^^^^^^^^^^^
   |

The first call to createUser() succeeds. The second call fails because a valid email argument isn’t included, which triggers the abort() action.

The response message is the notification response.

Notice that the query runs all the statements included in the transaction.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!