diff --git a/packages/exceptions/.gitignore b/packages/exceptions/.gitignore new file mode 100644 index 0000000..24d6831 --- /dev/null +++ b/packages/exceptions/.gitignore @@ -0,0 +1,71 @@ +# See https://www.dartlang.org/tools/private-files.html + +# Files and directories created by pub +.dart_tool +.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/ + +### Dart template +# See https://www.dartlang.org/tools/private-files.html + +# Files and directories created by pub + +# SDK 1.20 and later (no longer creates packages directories) + +# Older SDK versions +# (Include if the minimum SDK version specified in pubsepc.yaml is earlier than 1.20) +.project +.buildlog +**/packages/ + + +# Files created by dart2js +# (Most Dart developers will use pub build to compile Dart, use/modify these +# rules if you intend to use dart2js directly +# Convention is to use extension '.dart.js' for Dart compiled to Javascript to +# differentiate from explicit Javascript files) +*.dart.js +*.part.js +*.js.deps +*.js.map +*.info.json + +# Directory created by dartdoc + +# Don't commit pubspec lock file +# (Library packages only! Remove pattern if developing an application package) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: + +## VsCode +.vscode/ + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +.idea/ +/out/ +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/packages/exceptions/AUTHORS.md b/packages/exceptions/AUTHORS.md new file mode 100644 index 0000000..ac95ab5 --- /dev/null +++ b/packages/exceptions/AUTHORS.md @@ -0,0 +1,12 @@ +Primary Authors +=============== + +* __[Thomas Hii](dukefirehawk.apps@gmail.com)__ + + Thomas is the current maintainer of the code base. He has refactored and migrated the + code base to support NNBD. + +* __[Tobe O](thosakwe@gmail.com)__ + + Tobe has written much of the original code prior to NNBD migration. He has moved on and + is no longer involved with the project. diff --git a/packages/exceptions/CHANGELOG.md b/packages/exceptions/CHANGELOG.md new file mode 100644 index 0000000..1b7db3c --- /dev/null +++ b/packages/exceptions/CHANGELOG.md @@ -0,0 +1,72 @@ + +# Change Log + +## 8.1.1 + +* Updated repository link + +## 8.1.0 + +* Updated `lints` to 3.0.0 + +## 8.0.0 + +* Require Dart >= 3.0 + +## 7.0.0 + +* Require Dart >= 2.17 + +## 6.0.1 + +* Updated README + +## 6.0.0 + +* Require Dart >= 2.16 +* [**Breaking**] `error` for `AngelHttpException` is no longer mandatory + +## 5.0.0 + +* Skipped release + +## 4.0.0 + +* Skipped release + +## 3.1.0 + +* Upgraded to `lints` linter + +## 3.0.2 + +* Updated LICENSE link + +## 3.0.1 + +* Updated README + +## 3.0.0 + +* Migrated to support Dart >= 2.12 NNBD + +## 2.0.0 + +* Migrated to work with Dart >= 2.12 Non NNBD + +## 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. diff --git a/packages/exceptions/LICENSE b/packages/exceptions/LICENSE new file mode 100644 index 0000000..df5e063 --- /dev/null +++ b/packages/exceptions/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2021, dukefirehawk.com +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/exceptions/README.md b/packages/exceptions/README.md new file mode 100644 index 0000000..093ab5e --- /dev/null +++ b/packages/exceptions/README.md @@ -0,0 +1,8 @@ +# Angel3 Http Exception + +![Pub Version (including pre-releases)](https://img.shields.io/pub/v/angel3_http_exception?include_prereleases) +[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) +[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) +[![License](https://img.shields.io/github/license/dart-backend/angel)](https://github.com/dart-backend/angel/tree/master/packages/http_exception/LICENSE) + +Exception class that can be serialized to JSON and serialized to clients. Angel3's HTTP exception class. diff --git a/packages/exceptions/analysis_options.yaml b/packages/exceptions/analysis_options.yaml new file mode 100644 index 0000000..ea2c9e9 --- /dev/null +++ b/packages/exceptions/analysis_options.yaml @@ -0,0 +1 @@ +include: package:lints/recommended.yaml \ No newline at end of file diff --git a/packages/exceptions/example/main.dart b/packages/exceptions/example/main.dart new file mode 100644 index 0000000..ef7afe0 --- /dev/null +++ b/packages/exceptions/example/main.dart @@ -0,0 +1,3 @@ +import 'package:platform_exceptions/http_exception.dart'; + +void main() => throw HttpException.notFound(message: "Can't find that page!"); diff --git a/packages/exceptions/lib/http_exception.dart b/packages/exceptions/lib/http_exception.dart new file mode 100644 index 0000000..77ed1ae --- /dev/null +++ b/packages/exceptions/lib/http_exception.dart @@ -0,0 +1,121 @@ +library http_exception; + +//import 'package:dart2_constant/convert.dart'; +import 'dart:convert'; + +/// 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 HttpException implements Exception { + /// A list of errors that occurred when this exception was thrown. + final List errors = []; + + /// The error throw by exception. + dynamic error; + + /// 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; + + HttpException( + {this.message = '500 Internal Server Error', + this.stackTrace, + this.statusCode = 500, + this.error, + List errors = const []}) { + 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 HttpException.fromMap(Map data) { + return HttpException( + statusCode: (data['status_code'] ?? data['statusCode'] ?? 500) as int, + message: data['message']?.toString() ?? 'Internal Server Error', + errors: data['errors'] is Iterable + ? ((data['errors'] as Iterable).map((x) => x.toString()).toList()) + : [], + ); + } + + factory HttpException.fromJson(String str) => + HttpException.fromMap(json.decode(str) as Map); + + /// Throws a 400 Bad Request error, including an optional arrray of (validation?) + /// errors you specify. + factory HttpException.badRequest( + {String message = '400 Bad Request', + List errors = const []}) => + HttpException(message: message, errors: errors, statusCode: 400); + + /// Throws a 401 Not Authenticated error. + factory HttpException.notAuthenticated( + {String message = '401 Not Authenticated'}) => + HttpException(message: message, statusCode: 401); + + /// Throws a 402 Payment Required error. + factory HttpException.paymentRequired( + {String message = '402 Payment Required'}) => + HttpException(message: message, statusCode: 402); + + /// Throws a 403 Forbidden error. + factory HttpException.forbidden({String message = '403 Forbidden'}) => + HttpException(message: message, statusCode: 403); + + /// Throws a 404 Not Found error. + factory HttpException.notFound({String message = '404 Not Found'}) => + HttpException(message: message, statusCode: 404); + + /// Throws a 405 Method Not Allowed error. + factory HttpException.methodNotAllowed( + {String message = '405 Method Not Allowed'}) => + HttpException(message: message, statusCode: 405); + + /// Throws a 406 Not Acceptable error. + factory HttpException.notAcceptable( + {String message = '406 Not Acceptable'}) => + HttpException(message: message, statusCode: 406); + + /// Throws a 408 Timeout error. + factory HttpException.methodTimeout({String message = '408 Timeout'}) => + HttpException(message: message, statusCode: 408); + + /// Throws a 409 Conflict error. + factory HttpException.conflict({String message = '409 Conflict'}) => + HttpException(message: message, statusCode: 409); + + /// Throws a 422 Not Processable error. + factory HttpException.notProcessable( + {String message = '422 Not Processable'}) => + HttpException(message: message, statusCode: 422); + + /// Throws a 501 Not Implemented error. + factory HttpException.notImplemented( + {String message = '501 Not Implemented'}) => + HttpException(message: message, statusCode: 501); + + /// Throws a 503 Unavailable error. + factory HttpException.unavailable({String message = '503 Unavailable'}) => + HttpException(message: message, statusCode: 503); +} diff --git a/packages/exceptions/pubspec.yaml b/packages/exceptions/pubspec.yaml new file mode 100644 index 0000000..87e70f4 --- /dev/null +++ b/packages/exceptions/pubspec.yaml @@ -0,0 +1,9 @@ +name: platform_exceptions +version: 9.0.0 +description: Exception pakcage +homepage: https://angel3-framework.web.app/ +repository: https://github.com/dart-backend/angel/tree/master/packages/http_exception +environment: + sdk: '>=3.3.0 <4.0.0' +dev_dependencies: + lints: ^4.0.0 \ No newline at end of file