celest-dev/celest

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

marcglasberg opened this issue · 3 comments

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

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.

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.

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