diff --git a/.gitignore b/.gitignore index 4d2a4d6d..37664985 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ pubspec.lock # Directory created by dartdoc # If you don't generate documentation locally you can remove this line. doc/api/ + +.idea \ No newline at end of file diff --git a/lib/angel_http_exception.dart b/lib/angel_http_exception.dart new file mode 100644 index 00000000..bc2d266f --- /dev/null +++ b/lib/angel_http_exception.dart @@ -0,0 +1,114 @@ +library angel_http_exception; + +import 'dart:convert'; + +/// Basically the same as +/// [feathers-errors](https://github.com/feathersjs/feathers-errors). +class AngelHttpException implements Exception { + var error; + + /// A list of errors that occurred when this exception was thrown. + final List errors = []; + + /// The cause of this exception. + String message; + + /// The [StackTrace] associated with this error. + StackTrace stackTrace; + + /// An HTTP status code this exception will throw. + int statusCode; + + AngelHttpException(this.error, + {this.message: '500 Internal Server Error', + this.stackTrace, + this.statusCode: 500, + List errors: const []}) { + if (errors != null) { + this.errors.addAll(errors); + } + } + + Map toJson() { + return { + 'isError': true, + 'status_code': statusCode, + 'message': message, + 'errors': errors + }; + } + + Map toMap() => toJson(); + + @override + String toString() { + return "$statusCode: $message"; + } + + factory AngelHttpException.fromMap(Map data) { + return new AngelHttpException(null, + statusCode: data['status_code'] ?? data['statusCode'], + message: data['message'], + errors: data['errors']); + } + + factory AngelHttpException.fromJson(String json) => + new AngelHttpException.fromMap(JSON.decode(json)); + + /// Throws a 400 Bad Request error, including an optional arrray of (validation?) + /// errors you specify. + factory AngelHttpException.badRequest( + {String message: '400 Bad Request', List errors: const []}) => + new AngelHttpException(null, + message: message, errors: errors, statusCode: 400); + + /// Throws a 401 Not Authenticated error. + factory AngelHttpException.notAuthenticated( + {String message: '401 Not Authenticated'}) => + new AngelHttpException(null, message: message, statusCode: 401); + + /// Throws a 402 Payment Required error. + factory AngelHttpException.paymentRequired( + {String message: '402 Payment Required'}) => + new AngelHttpException(null, message: message, statusCode: 402); + + /// Throws a 403 Forbidden error. + factory AngelHttpException.forbidden({String message: '403 Forbidden'}) => + new AngelHttpException(null, message: message, statusCode: 403); + + /// Throws a 404 Not Found error. + factory AngelHttpException.notFound({String message: '404 Not Found'}) => + new AngelHttpException(null, message: message, statusCode: 404); + + /// Throws a 405 Method Not Allowed error. + factory AngelHttpException.methodNotAllowed( + {String message: '405 Method Not Allowed'}) => + new AngelHttpException(null, message: message, statusCode: 405); + + /// Throws a 406 Not Acceptable error. + factory AngelHttpException.notAcceptable( + {String message: '406 Not Acceptable'}) => + new AngelHttpException(null, message: message, statusCode: 406); + + /// Throws a 408 Timeout error. + factory AngelHttpException.methodTimeout({String message: '408 Timeout'}) => + new AngelHttpException(null, message: message, statusCode: 408); + + /// Throws a 409 Conflict error. + factory AngelHttpException.conflict({String message: '409 Conflict'}) => + new AngelHttpException(null, message: message, statusCode: 409); + + /// Throws a 422 Not Processable error. + factory AngelHttpException.notProcessable( + {String message: '422 Not Processable'}) => + new AngelHttpException(null, message: message, statusCode: 422); + + /// Throws a 501 Not Implemented error. + factory AngelHttpException.notImplemented( + {String message: '501 Not Implemented'}) => + new AngelHttpException(null, message: message, statusCode: 501); + + /// Throws a 503 Unavailable error. + factory AngelHttpException.unavailable({String message: '503 Unavailable'}) => + new AngelHttpException(null, message: message, statusCode: 503); +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 00000000..aa389824 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,7 @@ +name: angel_http_exception +version: 1.0.0 +description: Angel's HTTP exception class. +author: Tobe O +homepage: https://github.com/angel-dart/http_exception +environment: + sdk: ">=1.19.0" \ No newline at end of file