VeryGoodOpenSource / dart_frog

A fast, minimalistic backend framework for Dart 🎯
https://dartfrog.vgv.dev
MIT License
1.84k stars 148 forks source link

feat: add global exception catching #1256

Open wamynobe opened 8 months ago

wamynobe commented 8 months ago

Description

Clearly describe what you are looking to add. The more context the better.

Requirements

Additional Context

Add any other context or screenshots about the feature request go here.

alestiago commented 7 months ago

Hi @wamynobe 👋 ! I'm curious to know if you have a proposal on how this should be done 👀, for example, do you have an API in mind?

wamynobe commented 7 months ago

Hi @alestiago . I have some ideals.

void main() async {
  entrypoint.bootstrap(
    () async {
      final address = InternetAddress.tryParse('{{{host}}}') ?? InternetAddress.anyIPv6;
      final port = int.tryParse(Platform.environment['PORT'] ?? '{{{port}}}') ?? {{{port}}};{{#invokeCustomInit}}
      await entrypoint.init(address, port);{{/invokeCustomInit}}
      hotReload(() => createServer(address, port));
    }
  );

}

Can we add a bootstrap function in custom main file? This may help in some cases like using third-party SDK in order to log errors on my server. Then we can go like

void bootstrap(void Function() callback) async{
  runZonedGuarded(
    () => callback.call(),
    (error, stack) {
      // I want to log the error here
      log('Error: $error');
    },
  );
}

Or if you want to use SentrySDK for dart you can go like

void bootstrap(void Function() callback) async{
   await Sentry.init(
    (options) {
      options.dsn = 'https://example@sentry.io/example';
    },
    appRunner: callback,
  );
}

That's all! I'm new here so please forgive me if I've said anything wrong ^_^.

wamynobe commented 7 months ago

I am trying to figure out how to use Mason and how to customize these bricks. I want to check my ideal but I have not found a way to custom dart_frog_dev_server yet.

wamynobe commented 7 months ago

I checked dart_frog_dev_server brick but I have no ideal how to modify this file because of the line "// GENERATED CODE - DO NOT MODIFY BY HAND". I'm confused!

alestiago commented 7 months ago

@wamynobe thank you for taking an initiative here 💙 .

There is a CONTRIBUTING guide for updating Dart Frog CLI. There is no CONTRIBUTING guide for the bricks. I think we should have one. I may be able to provide one, however, you might have to bear with me, since I have other tasks with greater priority.

In terms of moving forward and adding global exception catching, it needs to be discussed with the team and evaluated. In the meantime, if you would like to work on a proof of concept, please do!

wamynobe commented 7 months ago

@alestiago Thank you for the instructions. I can now edit the server.dart file and it works as I expected. Thanks for pointing out the contribute section in the cli. I honestly overlooked it when trying to modify bricks and bundles. Regarding this issue, after discussing with the team, if there is anything I can help with, please ask!