2021-05-14 03:57:54 +00:00
|
|
|
library angel3_http_exception;
|
2017-09-22 14:00:03 +00:00
|
|
|
|
2021-03-05 08:32:47 +00:00
|
|
|
//import 'package:dart2_constant/convert.dart';
|
|
|
|
import 'dart:convert';
|
2017-09-22 14:00:03 +00:00
|
|
|
|
2019-02-11 00:18:12 +00:00
|
|
|
/// Exception class that can be serialized to JSON and serialized to clients.
|
|
|
|
/// Carries HTTP-specific metadata, like [statusCode].
|
|
|
|
///
|
|
|
|
/// Originally inspired by
|
2017-09-22 14:00:03 +00:00
|
|
|
/// [feathers-errors](https://github.com/feathersjs/feathers-errors).
|
|
|
|
class AngelHttpException implements Exception {
|
|
|
|
/// A list of errors that occurred when this exception was thrown.
|
|
|
|
final List<String> errors = [];
|
|
|
|
|
2022-02-13 16:48:37 +00:00
|
|
|
/// The cause of this exception.
|
|
|
|
dynamic error;
|
|
|
|
|
2017-09-22 14:00:03 +00:00
|
|
|
/// The cause of this exception.
|
2021-04-09 13:38:31 +00:00
|
|
|
String message;
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// The [StackTrace] associated with this error.
|
2021-03-09 16:41:42 +00:00
|
|
|
StackTrace? stackTrace;
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// An HTTP status code this exception will throw.
|
2021-04-09 13:38:31 +00:00
|
|
|
int statusCode;
|
2017-09-22 14:00:03 +00:00
|
|
|
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(
|
2019-02-11 00:18:12 +00:00
|
|
|
{this.message = '500 Internal Server Error',
|
2018-06-23 03:36:02 +00:00
|
|
|
this.stackTrace,
|
2019-02-11 00:18:12 +00:00
|
|
|
this.statusCode = 500,
|
|
|
|
List<String> errors = const []}) {
|
2021-04-09 13:38:31 +00:00
|
|
|
this.errors.addAll(errors);
|
2017-09-22 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Map toJson() {
|
|
|
|
return {
|
2019-02-11 00:18:12 +00:00
|
|
|
'is_error': true,
|
2017-09-22 14:00:03 +00:00
|
|
|
'status_code': statusCode,
|
|
|
|
'message': message,
|
|
|
|
'errors': errors
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Map toMap() => toJson();
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
2021-03-05 08:32:47 +00:00
|
|
|
return '$statusCode: $message';
|
2017-09-22 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
factory AngelHttpException.fromMap(Map data) {
|
2021-03-05 08:32:47 +00:00
|
|
|
return AngelHttpException(
|
2021-04-09 13:38:31 +00:00
|
|
|
statusCode: (data['status_code'] ?? data['statusCode'] ?? 500) as int,
|
|
|
|
message: data['message']?.toString() ?? 'Internal Server Error',
|
2018-07-09 16:09:01 +00:00
|
|
|
errors: data['errors'] is Iterable
|
|
|
|
? ((data['errors'] as Iterable).map((x) => x.toString()).toList())
|
2018-07-09 14:27:31 +00:00
|
|
|
: <String>[],
|
|
|
|
);
|
2017-09-22 14:00:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:36:02 +00:00
|
|
|
factory AngelHttpException.fromJson(String str) =>
|
2021-03-05 08:32:47 +00:00
|
|
|
AngelHttpException.fromMap(json.decode(str) as Map);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 400 Bad Request error, including an optional arrray of (validation?)
|
|
|
|
/// errors you specify.
|
|
|
|
factory AngelHttpException.badRequest(
|
2019-02-11 00:18:12 +00:00
|
|
|
{String message = '400 Bad Request',
|
|
|
|
List<String> errors = const []}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, errors: errors, statusCode: 400);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 401 Not Authenticated error.
|
|
|
|
factory AngelHttpException.notAuthenticated(
|
2019-02-11 00:18:12 +00:00
|
|
|
{String message = '401 Not Authenticated'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 401);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 402 Payment Required error.
|
|
|
|
factory AngelHttpException.paymentRequired(
|
2019-02-11 00:18:12 +00:00
|
|
|
{String message = '402 Payment Required'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 402);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 403 Forbidden error.
|
2019-02-11 00:18:12 +00:00
|
|
|
factory AngelHttpException.forbidden({String message = '403 Forbidden'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 403);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 404 Not Found error.
|
2019-02-11 00:18:12 +00:00
|
|
|
factory AngelHttpException.notFound({String message = '404 Not Found'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 404);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 405 Method Not Allowed error.
|
|
|
|
factory AngelHttpException.methodNotAllowed(
|
2019-02-11 00:18:12 +00:00
|
|
|
{String message = '405 Method Not Allowed'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 405);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 406 Not Acceptable error.
|
|
|
|
factory AngelHttpException.notAcceptable(
|
2019-02-11 00:18:12 +00:00
|
|
|
{String message = '406 Not Acceptable'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 406);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 408 Timeout error.
|
2019-02-11 00:18:12 +00:00
|
|
|
factory AngelHttpException.methodTimeout({String message = '408 Timeout'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 408);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 409 Conflict error.
|
2019-02-11 00:18:12 +00:00
|
|
|
factory AngelHttpException.conflict({String message = '409 Conflict'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 409);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 422 Not Processable error.
|
|
|
|
factory AngelHttpException.notProcessable(
|
2019-02-11 00:18:12 +00:00
|
|
|
{String message = '422 Not Processable'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 422);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 501 Not Implemented error.
|
|
|
|
factory AngelHttpException.notImplemented(
|
2019-02-11 00:18:12 +00:00
|
|
|
{String message = '501 Not Implemented'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 501);
|
2017-09-22 14:00:03 +00:00
|
|
|
|
|
|
|
/// Throws a 503 Unavailable error.
|
2019-02-11 00:18:12 +00:00
|
|
|
factory AngelHttpException.unavailable(
|
|
|
|
{String message = '503 Unavailable'}) =>
|
2022-02-13 16:48:37 +00:00
|
|
|
AngelHttpException(message: message, statusCode: 503);
|
2017-09-22 14:00:03 +00:00
|
|
|
}
|