nice_exception
A brick to create nice exceptions using Freezed.
How to use š
mason make nice_exception --exception_name custom
Variables āØ
Variable | Description | Default | Type |
---|---|---|---|
exception_name | The name of the exception | custom | string |
error_types | Exception error types | [network,unauthorized] | array |
Outputs š¦
--exception_name custom
āā custom_exception.dart
āā custom_exception.freezed.dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'custom_exception.freezed.dart';
@freezed
class CustomException with _$CustomException implements Exception {
const factory CustomException.otherError({
required Object error,
required StackTrace stacktrace,
}) = _OtherError;
const factory CustomException.networkError({
@Default('NetworkError') String? message,
}) = _NetworkError;
const factory CustomException.unauthorizedError({
@Default('UnauthorizedError') String? message,
}) = _UnauthorizedError;
}
After you use it inside a repo or service you can handle all possible errors like this
void main(){
try{
yourRepoOrService.fetchSomeData();
} on CustomException catch (e){
e.when(
networkError:(message)=>//handle this.
unauthenticated:(message)=>//handle this.
otherError:(error,stacktrace)=>//handle this.
);
}
}
A new brick created with the Mason CLI.
Generated by mason š§±
Getting Started š
This is a starting point for a new brick. A few resources to get you started if this is your first brick template: