veilid/veilid-flutter/lib/veilid_api_exception.dart

252 lines
6.5 KiB
Dart
Raw Normal View History

2023-07-05 22:48:06 +00:00
import 'package:freezed_annotation/freezed_annotation.dart';
2023-05-29 19:24:57 +00:00
//////////////////////////////////////
/// VeilidAPIException
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
abstract class VeilidAPIException implements Exception {
2023-07-26 19:12:28 +00:00
factory VeilidAPIException.fromJson(dynamic j) {
final json = j as Map<String, dynamic>;
switch (json['kind']! as String) {
2023-07-26 18:20:17 +00:00
case 'NotInitialized':
2023-05-29 19:24:57 +00:00
{
return VeilidAPIExceptionNotInitialized();
}
2023-07-26 18:20:17 +00:00
case 'AlreadyInitialized':
2023-05-29 19:24:57 +00:00
{
return VeilidAPIExceptionAlreadyInitialized();
}
2023-07-26 18:20:17 +00:00
case 'Timeout':
2023-05-29 19:24:57 +00:00
{
return VeilidAPIExceptionTimeout();
}
2023-07-26 18:20:17 +00:00
case 'TryAgain':
2023-05-29 19:24:57 +00:00
{
2023-10-21 19:00:50 +00:00
return VeilidAPIExceptionTryAgain(json['message']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'Shutdown':
2023-05-29 19:24:57 +00:00
{
return VeilidAPIExceptionShutdown();
}
2023-07-26 18:20:17 +00:00
case 'InvalidTarget':
2023-05-29 19:24:57 +00:00
{
2023-10-21 19:00:50 +00:00
return VeilidAPIExceptionInvalidTarget(json['message']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'NoConnection':
2023-05-29 19:24:57 +00:00
{
2023-07-26 19:12:28 +00:00
return VeilidAPIExceptionNoConnection(json['message']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'KeyNotFound':
2023-05-29 19:24:57 +00:00
{
2023-07-26 19:12:28 +00:00
return VeilidAPIExceptionKeyNotFound(json['key']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'Internal':
2023-05-29 19:24:57 +00:00
{
2023-07-26 19:12:28 +00:00
return VeilidAPIExceptionInternal(json['message']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'Unimplemented':
2023-05-29 19:24:57 +00:00
{
2023-07-26 19:12:28 +00:00
return VeilidAPIExceptionUnimplemented(
json['unimplemented']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'ParseError':
2023-05-29 19:24:57 +00:00
{
2023-07-26 19:12:28 +00:00
return VeilidAPIExceptionParseError(
json['message']! as String, json['value']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'InvalidArgument':
2023-05-29 19:24:57 +00:00
{
2023-07-26 19:12:28 +00:00
return VeilidAPIExceptionInvalidArgument(json['context']! as String,
json['argument']! as String, json['value']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'MissingArgument':
2023-05-29 19:24:57 +00:00
{
return VeilidAPIExceptionMissingArgument(
2023-07-26 19:12:28 +00:00
json['context']! as String, json['argument']! as String);
2023-05-29 19:24:57 +00:00
}
2023-07-26 18:20:17 +00:00
case 'Generic':
2023-05-29 19:24:57 +00:00
{
2023-07-26 19:12:28 +00:00
return VeilidAPIExceptionGeneric(json['message']! as String);
2023-05-29 19:24:57 +00:00
}
default:
{
throw VeilidAPIExceptionInternal(
2023-07-26 19:12:28 +00:00
"Invalid VeilidAPIException type: ${json['kind']! as String}");
2023-05-29 19:24:57 +00:00
}
}
}
String toDisplayError();
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionNotInitialized implements VeilidAPIException {
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: NotInitialized';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Not initialized';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionAlreadyInitialized implements VeilidAPIException {
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: AlreadyInitialized';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Already initialized';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionTimeout implements VeilidAPIException {
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: Timeout';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Timeout';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionTryAgain implements VeilidAPIException {
2023-10-21 19:00:50 +00:00
//
const VeilidAPIExceptionTryAgain(this.message);
final String message;
2023-05-29 19:24:57 +00:00
@override
2023-10-21 19:00:50 +00:00
String toString() => 'VeilidAPIException: TryAgain (message: $message)';
2023-05-29 19:24:57 +00:00
@override
2023-10-21 19:00:50 +00:00
String toDisplayError() => 'Try again: (message: $message)';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionShutdown implements VeilidAPIException {
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: Shutdown';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Currently shut down';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionInvalidTarget implements VeilidAPIException {
2023-10-21 19:00:50 +00:00
//
const VeilidAPIExceptionInvalidTarget(this.message);
final String message;
2023-05-29 19:24:57 +00:00
@override
2023-10-21 19:00:50 +00:00
String toString() => 'VeilidAPIException: InvalidTarget (message: $message)';
2023-05-29 19:24:57 +00:00
@override
2023-10-21 19:00:50 +00:00
String toDisplayError() => 'Invalid target: (message: $message)';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionNoConnection implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionNoConnection(this.message);
2023-05-29 19:24:57 +00:00
final String message;
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: NoConnection (message: $message)';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'No connection: $message';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionKeyNotFound implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionKeyNotFound(this.key);
2023-05-29 19:24:57 +00:00
final String key;
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: KeyNotFound (key: $key)';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Key not found: $key';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionInternal implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionInternal(this.message);
2023-05-29 19:24:57 +00:00
final String message;
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: Internal ($message)';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Internal error: $message';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionUnimplemented implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionUnimplemented(this.message);
2023-05-29 19:24:57 +00:00
final String message;
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: Unimplemented ($message)';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Unimplemented: $message';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionParseError implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionParseError(this.message, this.value);
2023-05-29 19:24:57 +00:00
final String message;
final String value;
@override
2023-07-26 19:12:28 +00:00
String toString() =>
'VeilidAPIException: ParseError ($message)\n value: $value';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Parse error: $message';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionInvalidArgument implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionInvalidArgument(
this.context, this.argument, this.value);
2023-05-29 19:24:57 +00:00
final String context;
final String argument;
final String value;
@override
2023-07-30 19:57:06 +00:00
String toString() => 'VeilidAPIException: InvalidArgument'
' ($context:$argument)\n value: $value';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Invalid argument for $context: $argument';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionMissingArgument implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionMissingArgument(this.context, this.argument);
2023-05-29 19:24:57 +00:00
final String context;
final String argument;
@override
2023-07-26 19:12:28 +00:00
String toString() =>
'VeilidAPIException: MissingArgument ($context:$argument)';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => 'Missing argument for $context: $argument';
2023-05-29 19:24:57 +00:00
}
2023-07-05 22:48:06 +00:00
@immutable
2023-05-29 19:24:57 +00:00
class VeilidAPIExceptionGeneric implements VeilidAPIException {
2023-07-26 18:20:17 +00:00
//
const VeilidAPIExceptionGeneric(this.message);
2023-05-29 19:24:57 +00:00
final String message;
@override
2023-07-26 18:20:17 +00:00
String toString() => 'VeilidAPIException: Generic (message: $message)';
2023-05-29 19:24:57 +00:00
@override
2023-07-26 18:20:17 +00:00
String toDisplayError() => message;
2023-05-29 19:24:57 +00:00
}