Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
feat: add custom exceptions (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanwlee authored Jun 7, 2024
1 parent 71fe418 commit bd54813
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 89 deletions.
3 changes: 3 additions & 0 deletions lib/src/http_client.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export './http_client/exceptions/http_exceptions.dart';
export './http_client/exceptions/token_exceptions.dart';
export './http_client/exceptions/validation_exceptions.dart';
export './http_client/models/create_exchange_request.dart';
export './http_client/models/exchange.dart';
export './http_client/models/get_offerings_filter.dart';
Expand Down
41 changes: 41 additions & 0 deletions lib/src/http_client/exceptions/http_exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class RequestError implements Exception {
final String message;
final String? url;
final Exception? cause;

RequestError({
required this.message,
this.url,
this.cause,
});

String get errorType => 'RequestError';

@override
String toString() => [
'$errorType: $message',
if (url != null) 'Url: $url',
if (cause != null) 'Caused by: $cause',
].join('\n');
}

class ResponseError implements Exception {
final String message;
final int? status;
final String? body;

ResponseError({
required this.message,
this.status,
this.body,
});

String get errorType => 'ResponseError';

@override
String toString() => [
'$errorType: $message',
if (status != null) 'Status: $status',
if (body != null) 'Body: $body',
].join('\n');
}
83 changes: 83 additions & 0 deletions lib/src/http_client/exceptions/token_exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// TODO(ethan-tbd): move to web5-dart when kcc is added to web5-dart
class _TbdexTokenError implements Exception {
final String message;
final Exception? cause;

_TbdexTokenError({
required this.message,
this.cause,
});

String get errorType => 'TbdexValidationError';

@override
String toString() => [
'$errorType: $message',
if (cause != null) 'Caused by: $cause',
].join('\n');
}

class RequestTokenError extends _TbdexTokenError {
RequestTokenError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'RequestTokenError';
}

class RequestTokenSigningError extends RequestTokenError {
RequestTokenSigningError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'RequestTokenSigningError';
}

class RequestTokenVerificationError extends RequestTokenError {
RequestTokenVerificationError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'RequestTokenVerificationError';
}

class RequestTokenMissingClaimsError extends RequestTokenError {
RequestTokenMissingClaimsError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'RequestTokenMissingClaimsError';
}

class RequestTokenAudienceMismatchError extends RequestTokenError {
RequestTokenAudienceMismatchError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'RequestTokenAudienceMismatchError';
}
56 changes: 56 additions & 0 deletions lib/src/http_client/exceptions/validation_exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class _TbdexValidationError implements Exception {
final String message;
final Exception? cause;

_TbdexValidationError({
required this.message,
this.cause,
});

String get errorType => 'TbdexValidationError';

@override
String toString() => [
'$errorType: $message',
if (cause != null) 'Caused by: $cause',
].join('\n');
}

class ValidationError extends _TbdexValidationError {
ValidationError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'ValidationError';
}

class InvalidDidError extends ValidationError {
InvalidDidError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'InvalidDidError';
}

class MissingServiceEndpointError extends ValidationError {
MissingServiceEndpointError({
required String message,
Exception? cause,
}) : super(
message: message,
cause: cause,
);

@override
String get errorType => 'MissingServiceEndpointError';
}
Loading

0 comments on commit bd54813

Please sign in to comment.