Add 'packages/http_exception/' from commit 'fef0c681593f12caf5c1f7b0f3206c7bace566e2'
git-subtree-dir: packages/http_exception git-subtree-mainline:58e42256e5
git-subtree-split:fef0c68159
This commit is contained in:
commit
3a14263a6f
8 changed files with 194 additions and 0 deletions
14
packages/http_exception/.gitignore
vendored
Normal file
14
packages/http_exception/.gitignore
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
# See https://www.dartlang.org/tools/private-files.html
|
||||
|
||||
# Files and directories created by pub
|
||||
.packages
|
||||
.pub/
|
||||
build/
|
||||
# If you're building an application, you may want to check-in your pubspec.lock
|
||||
pubspec.lock
|
||||
|
||||
# Directory created by dartdoc
|
||||
# If you don't generate documentation locally you can remove this line.
|
||||
doc/api/
|
||||
|
||||
.idea
|
12
packages/http_exception/CHANGELOG.md
Normal file
12
packages/http_exception/CHANGELOG.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
# 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`.
|
||||
|
||||
# 1.0.0+2
|
||||
* Added a backwards-compatible way to cast the `errors` List.
|
||||
|
||||
# 1.0.0+1
|
||||
* Dart 2 updates.
|
21
packages/http_exception/LICENSE
Normal file
21
packages/http_exception/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2017 The Angel Framework
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
5
packages/http_exception/README.md
Normal file
5
packages/http_exception/README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# http_exception
|
||||
[![Pub](https://img.shields.io/pub/v/angel_http_exception.svg)](https://pub.dartlang.org/packages/angel_http_exception)
|
||||
|
||||
Exception class that can be serialized to JSON and serialized to clients.
|
||||
Angel's HTTP exception class.
|
4
packages/http_exception/analysis_options.yaml
Normal file
4
packages/http_exception/analysis_options.yaml
Normal file
|
@ -0,0 +1,4 @@
|
|||
include: package:pedantic/analysis_options.yaml
|
||||
analyzer:
|
||||
strong-mode:
|
||||
implicit-casts: false
|
4
packages/http_exception/example/main.dart
Normal file
4
packages/http_exception/example/main.dart
Normal file
|
@ -0,0 +1,4 @@
|
|||
import 'package:angel_http_exception/angel_http_exception.dart';
|
||||
|
||||
void main() =>
|
||||
throw new AngelHttpException.notFound(message: "Can't find that page!");
|
123
packages/http_exception/lib/angel_http_exception.dart
Normal file
123
packages/http_exception/lib/angel_http_exception.dart
Normal file
|
@ -0,0 +1,123 @@
|
|||
library angel_http_exception;
|
||||
|
||||
import 'package:dart2_constant/convert.dart';
|
||||
|
||||
/// 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;
|
||||
|
||||
/// A list of errors that occurred when this exception was thrown.
|
||||
final List<String> 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<String> errors = const []}) {
|
||||
if (errors != null) {
|
||||
this.errors.addAll(errors);
|
||||
}
|
||||
}
|
||||
|
||||
Map toJson() {
|
||||
return {
|
||||
'is_error': 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']) as int,
|
||||
message: data['message']?.toString(),
|
||||
errors: data['errors'] is Iterable
|
||||
? ((data['errors'] as Iterable).map((x) => x.toString()).toList())
|
||||
: <String>[],
|
||||
);
|
||||
}
|
||||
|
||||
factory AngelHttpException.fromJson(String str) =>
|
||||
new AngelHttpException.fromMap(json.decode(str) as Map);
|
||||
|
||||
/// 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 []}) =>
|
||||
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);
|
||||
}
|
11
packages/http_exception/pubspec.yaml
Normal file
11
packages/http_exception/pubspec.yaml
Normal file
|
@ -0,0 +1,11 @@
|
|||
name: angel_http_exception
|
||||
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
|
||||
dev_dependencies:
|
||||
pedantic: ^1.0.0
|
Loading…
Reference in a new issue