Skip to content

Allow Celest environment in the backend #47

Closed
@marcglasberg

Description

@marcglasberg

In a cloud function, if I check celest.currentEnvironment it throws me an error:

An unexpected error occurred: Bad state: Celest has not been initialized. Make sure to call `celest.init()` at the start of your `main` method.

However, it's very useful to know, in the backend code, if it's running locally or not. For example, suppose an admin function that should only work locally (and staging) but not in production. This function can be used to set up the database to some initial state for testing purposes:

Future<void> setDatabase(Portfolio portfolio, Iterable<AvailableStock> availableStocks) async {
   _assertIsNotProduction();
   db.setState(portfolio, availableStocks);
}

/// Some admin functions should NOT work in production.
/// Those that do should make sure it's really the admin who's calling them.
void _assertIsNotProduction() {
   if (celest.currentEnvironment == CelestEnvironment.production)
     throw Exception('This function should only be used in local and staging environments.');
}

Since the celest object is visible from the function code, I think it should have this information (and other information, like the cookies #40 maybe). But if you try to access celest.functions from inside a function it should fail with an appropriate error (and not Celest has not been initialized. anyway).

Metadata

Metadata

Assignees

No one assigned

    Labels

    triagingInitial investigation into the issue

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions