veilid/veilid-flutter/lib/veilid_api_exception.dart
Christien Rioux f91a350bfc lint work
2023-07-26 14:20:17 -04:00

247 lines
5.9 KiB
Dart

import 'package:freezed_annotation/freezed_annotation.dart';
//////////////////////////////////////
/// VeilidAPIException
@immutable
abstract class VeilidAPIException implements Exception {
factory VeilidAPIException.fromJson(dynamic json) {
switch (json['kind']) {
case 'NotInitialized':
{
return VeilidAPIExceptionNotInitialized();
}
case 'AlreadyInitialized':
{
return VeilidAPIExceptionAlreadyInitialized();
}
case 'Timeout':
{
return VeilidAPIExceptionTimeout();
}
case 'TryAgain':
{
return VeilidAPIExceptionTryAgain();
}
case 'Shutdown':
{
return VeilidAPIExceptionShutdown();
}
case 'InvalidTarget':
{
return VeilidAPIExceptionInvalidTarget();
}
case 'NoConnection':
{
return VeilidAPIExceptionNoConnection(json['message']);
}
case 'KeyNotFound':
{
return VeilidAPIExceptionKeyNotFound(json['key']);
}
case 'Internal':
{
return VeilidAPIExceptionInternal(json['message']);
}
case 'Unimplemented':
{
return VeilidAPIExceptionUnimplemented(json['unimplemented']);
}
case 'ParseError':
{
return VeilidAPIExceptionParseError(json['message'], json['value']);
}
case 'InvalidArgument':
{
return VeilidAPIExceptionInvalidArgument(
json['context'], json['argument'], json['value']);
}
case 'MissingArgument':
{
return VeilidAPIExceptionMissingArgument(
json['context'], json['argument']);
}
case 'Generic':
{
return VeilidAPIExceptionGeneric(json['message']);
}
default:
{
throw VeilidAPIExceptionInternal(
"Invalid VeilidAPIException type: ${json['kind']}");
}
}
}
String toDisplayError();
}
@immutable
class VeilidAPIExceptionNotInitialized implements VeilidAPIException {
@override
String toString() => 'VeilidAPIException: NotInitialized';
@override
String toDisplayError() => 'Not initialized';
}
@immutable
class VeilidAPIExceptionAlreadyInitialized implements VeilidAPIException {
@override
String toString() => 'VeilidAPIException: AlreadyInitialized';
@override
String toDisplayError() => 'Already initialized';
}
@immutable
class VeilidAPIExceptionTimeout implements VeilidAPIException {
@override
String toString() => 'VeilidAPIException: Timeout';
@override
String toDisplayError() => 'Timeout';
}
@immutable
class VeilidAPIExceptionTryAgain implements VeilidAPIException {
@override
String toString() => 'VeilidAPIException: TryAgain';
@override
String toDisplayError() => 'Try again';
}
@immutable
class VeilidAPIExceptionShutdown implements VeilidAPIException {
@override
String toString() => 'VeilidAPIException: Shutdown';
@override
String toDisplayError() => 'Currently shut down';
}
@immutable
class VeilidAPIExceptionInvalidTarget implements VeilidAPIException {
@override
String toString() => 'VeilidAPIException: InvalidTarget';
@override
String toDisplayError() => 'Invalid target';
}
@immutable
class VeilidAPIExceptionNoConnection implements VeilidAPIException {
//
const VeilidAPIExceptionNoConnection(this.message);
final String message;
@override
String toString() => 'VeilidAPIException: NoConnection (message: $message)';
@override
String toDisplayError() => 'No connection: $message';
}
@immutable
class VeilidAPIExceptionKeyNotFound implements VeilidAPIException {
//
const VeilidAPIExceptionKeyNotFound(this.key);
final String key;
@override
String toString() => 'VeilidAPIException: KeyNotFound (key: $key)';
@override
String toDisplayError() => 'Key not found: $key';
}
@immutable
class VeilidAPIExceptionInternal implements VeilidAPIException {
//
const VeilidAPIExceptionInternal(this.message);
final String message;
@override
String toString() => 'VeilidAPIException: Internal ($message)';
@override
String toDisplayError() => 'Internal error: $message';
}
@immutable
class VeilidAPIExceptionUnimplemented implements VeilidAPIException {
//
const VeilidAPIExceptionUnimplemented(this.message);
final String message;
@override
String toString() => 'VeilidAPIException: Unimplemented ($message)';
@override
String toDisplayError() => 'Unimplemented: $message';
}
@immutable
class VeilidAPIExceptionParseError implements VeilidAPIException {
//
const VeilidAPIExceptionParseError(this.message, this.value);
final String message;
final String value;
@override
String toString() => 'VeilidAPIException: ParseError ($message)\n value: $value';
@override
String toDisplayError() => 'Parse error: $message';
}
@immutable
class VeilidAPIExceptionInvalidArgument implements VeilidAPIException {
//
const VeilidAPIExceptionInvalidArgument(
this.context, this.argument, this.value);
final String context;
final String argument;
final String value;
@override
String toString() => 'VeilidAPIException: InvalidArgument ($context:$argument)\n value: $value';
@override
String toDisplayError() => 'Invalid argument for $context: $argument';
}
@immutable
class VeilidAPIExceptionMissingArgument implements VeilidAPIException {
//
const VeilidAPIExceptionMissingArgument(this.context, this.argument);
final String context;
final String argument;
@override
String toString() => 'VeilidAPIException: MissingArgument ($context:$argument)';
@override
String toDisplayError() => 'Missing argument for $context: $argument';
}
@immutable
class VeilidAPIExceptionGeneric implements VeilidAPIException {
//
const VeilidAPIExceptionGeneric(this.message);
final String message;
@override
String toString() => 'VeilidAPIException: Generic (message: $message)';
@override
String toDisplayError() => message;
}