celest-dev / celest

The Flutter cloud platform
https://celest.dev
Other
232 stars 12 forks source link

Explain in the docs that Celest can serialize an exception imported from another package #39

Open marcglasberg opened 4 months ago

marcglasberg commented 4 months ago

Users of my Async Redux package can display an error dialog to the user by simply throwing a UserException which is provided by the package itself. For example:

class SellStockAction extends ReduxAction {
  final Stock stock;
  final int howMany;

  AppState? reduce() {
     if (state.stocks.contain(stock)) return state.copy(stocks: stocks.sell(howMany));
     else throw UserException('You don't have any stocks of ${stock.ticker} to sell.');
     }
  }

But now I want to modify this code to create a Celest function called sell that throws a UserException internally:

class SellStockAction extends ReduxAction {
  final Stock stock;
  final int howMany;

  Future<AppState?> reduce() async {
     var stocks = await celest.functions.stocks.sell(stock, howMany); // This function may throw UserException.
     return state.copy(stocks: stocks);
     }
  }

Since UserException is defined in the Async Redux package, I can't move it to the exceptions directory.

Is it possible to force Celest to accept/serialize an exception that I'll import from another package?

dnys1 commented 4 months ago

Hi @marcglasberg, the condition for custom models and exceptions is that they be exported from models.dart/exceptions.dart, but do not have to be defined within.

So, you could have the following in exceptions.dart (although you'd run into #35 because of Object?).

export 'package:async_redux/async_redux.dart' show UserException;

// ... Other custom exception types.

I understand this requirement is a bit confusing and we're looking at better alternatives. In the meantime, I will go through our docs and see how we can better explain this.

marcglasberg commented 4 months ago

That's great! I renamed this issue to Explain in the docs that Celest can serialize an exception imported from another package. Just make it clear in the documentation.

abdallahshaban557 commented 4 months ago

Aaaand that's my cue. I'll get it updated ASAP!