close method

Future<int?> close({
  1. dynamic error,
  2. StackTrace? stackTrace,
})

Closes the session. This method should only be called if you have manually created a the Session e.g. by calling createSession on Serverpod. Closing the session finalizes and writes logs to the database. After a session has been closed, you should not call any more methods on it. Optionally pass in an error/exception and stackTrace if the session ended with an error and it should be written to the logs. Returns the session id, if the session has been logged to the database.

Implementation

Future<int?> close({
  dynamic error,
  StackTrace? stackTrace,
}) async {
  if (_closed) return null;
  _closed = true;

  try {
    server.messageCentral.removeListenersForSession(this);
    return await server.serverpod.logManager.finalizeSessionLog(
      this,
      exception: error == null ? null : '$error',
      stackTrace: stackTrace,
      authenticatedUserId: _authenticatedUser,
    );
  } catch (e, stackTrace) {
    stderr.writeln('Failed to close session: $e');
    stderr.writeln('$stackTrace');
  }
  return null;
}