csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 96 forks source link

Handle `errorBuilder` with Error Type #355

Closed wisnuwiry closed 2 years ago

wisnuwiry commented 2 years ago

Description

When an error occurs when navigation eg the route does not exist or eg, queryParameter or params is invalid, or also problems about the assertion can be distinguished when in errorBuilder.

So when the error handle in errorBuilder I can render a specific page according to the error type.

Currently

image

For the current version I only get exception info when there is an error when navigating, and I don't know what type of exception it is and what the error is

Expected

The following might help an example of implementing an error handle:

enum GoRouterErrorType { notFound, paramInvalid, queryParamInvalid, ... }
 errorBuilder: (context, state) {
      // `state.error` the type is `GoRouterErrorType`
      // `state.exception` the type is `GoRouterException` (custom exception) 
 },
csells commented 2 years ago

I believe that the errorBuilder has all of the information that it can provide. Can you provide a minimal repro so that I can track it down?

diegoveloper commented 2 years ago

hey @csells , I believe he is talking about custom exceptions, currently is just returning a generic exception Exception with a description message.

https://github.com/csells/go_router/search?q=throw

Maybe would be a good idea to implement custom exceptions like @wisnuwiry mentioned, otherwise we have to parse the message and try to understand what kind of exception is in order to display an error page.

csells commented 2 years ago

Custom exceptions should come through just fine. Do you have a sample where they don't?

diegoveloper commented 2 years ago

Custom exceptions should come through just fine. Do you have a sample where they don't?

Hey Chris, when we have any exception related to navigation, do we receive a Custom Exception with some kind of type? or it's just a Generic Exception (Exception class) with the error details?

I think @wisnuwiry is talking about my last question.

wisnuwiry commented 2 years ago

Sorry, @csells @diegoveloper I was busy earlier,

As an example of implementing a custom exception such as the dio plugin as below:

image

And if implemented in go_router it might be like this:

abstract class GoRouterException implements Exception {
  const GoRouterException({
    required this.message,
    this.details,
  });

  final String message;
  final Object? details;
}

class GoRouterNotFoundException extends GoRouterException {
  const GoRouterNotFoundException({
    required String message,
    Object? details,
  }) : super(
          message: message,
          details: details,
        );
}

// And add some exceptions according to the error type in `go_router`.

...

// in go_router_state.dart 

/// The route state during routing.
class GoRouterState {

  ....

 // Update `Exception` to custom exception `GoRouterException`
  /// The error associated with this sub-route.
  final GoRouterException? error;

 ...
}

...

// And when in errorBuilder I can check that the error occurred because of what and the type can be specific based on the error type of the exception:

final _router = GoRouter(
    ...
    errorBuilder: (context, state) {
       if(state.error is GoRouterNotFoundException){
         return NotFoundPage(state.error?.message);
       }

       return DefaultErrorPage(state.error);
   }
  );

This is more or less what I wanted, thanks for responding 🙏