This commit is contained in:
Tobe O 2019-02-10 19:18:12 -05:00
parent c025992f47
commit fc5df85b9b
4 changed files with 32 additions and 20 deletions

View file

@ -1,3 +1,7 @@
# 1.1.0
* Emit `is_error` and `status_code` in `toJson()`.
* No more `camelCase` at all.
# 1.0.0+3
* Slightly relax the deserialization of `errors`.

View file

@ -1,3 +1,4 @@
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false

View file

@ -2,7 +2,10 @@ library angel_http_exception;
import 'package:dart2_constant/convert.dart';
/// Basically the same as
/// Exception class that can be serialized to JSON and serialized to clients.
/// Carries HTTP-specific metadata, like [statusCode].
///
/// Originally inspired by
/// [feathers-errors](https://github.com/feathersjs/feathers-errors).
class AngelHttpException implements Exception {
var error;
@ -20,10 +23,10 @@ class AngelHttpException implements Exception {
int statusCode;
AngelHttpException(this.error,
{this.message: '500 Internal Server Error',
{this.message = '500 Internal Server Error',
this.stackTrace,
this.statusCode: 500,
List<String> errors: const []}) {
this.statusCode = 500,
List<String> errors = const []}) {
if (errors != null) {
this.errors.addAll(errors);
}
@ -31,7 +34,7 @@ class AngelHttpException implements Exception {
Map toJson() {
return {
'isError': true,
'is_error': true,
'status_code': statusCode,
'message': message,
'errors': errors
@ -62,57 +65,59 @@ class AngelHttpException implements Exception {
/// Throws a 400 Bad Request error, including an optional arrray of (validation?)
/// errors you specify.
factory AngelHttpException.badRequest(
{String message: '400 Bad Request', List<String> errors: const []}) =>
{String message = '400 Bad Request',
List<String> 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'}) =>
{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'}) =>
{String message = '402 Payment Required'}) =>
new AngelHttpException(null, message: message, statusCode: 402);
/// Throws a 403 Forbidden error.
factory AngelHttpException.forbidden({String message: '403 Forbidden'}) =>
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'}) =>
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'}) =>
{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'}) =>
{String message = '406 Not Acceptable'}) =>
new AngelHttpException(null, message: message, statusCode: 406);
/// Throws a 408 Timeout error.
factory AngelHttpException.methodTimeout({String message: '408 Timeout'}) =>
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'}) =>
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'}) =>
{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'}) =>
{String message = '501 Not Implemented'}) =>
new AngelHttpException(null, message: message, statusCode: 501);
/// Throws a 503 Unavailable error.
factory AngelHttpException.unavailable({String message: '503 Unavailable'}) =>
factory AngelHttpException.unavailable(
{String message = '503 Unavailable'}) =>
new AngelHttpException(null, message: message, statusCode: 503);
}

View file

@ -1,9 +1,11 @@
name: angel_http_exception
version: 1.0.0+3
description: Angel's HTTP exception class.
version: 1.1.0
description: Exception class that can be serialized to JSON and serialized to clients.
author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/http_exception
environment:
sdk: ">=1.19.0 <3.0.0"
dependencies:
dart2_constant: ^1.0.0
dart2_constant: ^1.0.0
dev_dependencies:
pedantic: ^1.0.0