Skip to content

Commit 341b92c

Browse files
committed
pumb to 0.0.2
1 parent 1f17870 commit 341b92c

12 files changed

+39
-72
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.0.2
2+
3+
- support 404 `LNotFoundException`
4+
- support 500 `LServerException`
5+
- support 422 `LValidationException`
6+
- unit tests
7+
18
## 0.0.1-beta
29

310
- Initial version.

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22

33
# **`Part of Queen Packages 👑`**
44

5-
[![style: lint](https://img.shields.io/badge/style-lint-4BC0F5.svg)](https://pub.dev/packages/lint)
6-
75
# Motivation
86

97
- since we work with APIs built with `Laravel` almost daily
10-
- i have built this package to easily wrap the validation exception which laravel might throw
11-
- you can easily know which fields got rejected or have failed and there messages
8+
- i have built this package to easily wrap the validation , NotFound , Internal Server Error exceptions which laravel might throw
9+
- you can easily know which fields got rejected or have failed and there messages in case of validation
10+
- in laravel debug mode extract the message easily with the stack trace
11+
- also you can build your custom exception if needed
1212

1313
# Content
1414

15-
- `LaravelException` Exception class which will parse the laravel response
15+
- `LaravelException` abstract class which will parse the laravel response
16+
- `LValidationException` Exception for `422` status code use the constructor to create objects
17+
- `LServerException` Exception for `500` status code use the `parse(Map)` factory to create objects
18+
- `LNotFoundException` Exception for `404` status code use the `parse(Map)` factory to create objects
1619

1720
# example
1821

1922
```dart
20-
// 422
21-
if(res.statusCode == HttpStatus.unprocessableEntity){
22-
throw LaravelException.parse(res.data);
23+
24+
if(res.statusCode == 422){
25+
throw LValidationException(res.data);
26+
}else if(res.statusCode == 500){
27+
throw LServerException.parse(res.data);
28+
}else if(res.statusCode == 404){
29+
throw LNotFoundException.parse(res.data);
2330
}
2431
2532
```
2633

27-
# Exception props 🧬
28-
29-
- `keys` contains the failed input keys in the exception object
30-
- `errorMessages` contains the failed input keys in the exception object
31-
- `firstErrorMessage` return the first failure message
32-
- `toString()` convert the object to one big error message
33-
3434
# What is next ?
3535

3636
- [ ] dio interceptor

lib/laravel_exception.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/// Support for doing something royal.
22
library laravel_exception;
33

4+
export 'src/exceptions/imp.dart';
45
export 'src/exceptions/internal_server_error.dart';
5-
export 'src/extractors.dart';
66
export 'src/exceptions/not_found.dart';
77
export 'src/exceptions/validation_err.dart';
8-
export 'src/exceptions/imp.dart';

lib/src/dio_interceptor.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// @override
99
// void onResponse(Response response, ResponseInterceptorHandler handler) {
1010
// //TODO:: check if the backend is laravel
11+
// //TODO:: interceptors wont catch the thrown the exception instead will skip it
1112
// log(response.statusCode.toString());
1213
// switch (response.statusCode) {
1314
// case 500:

lib/src/exceptions/imp.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ import 'package:equatable/equatable.dart';
22

33
/// * " Long Live The Queen "
44
5-
abstract class LaravelException<T> with EquatableMixin implements Exception {
5+
abstract class LaravelException with EquatableMixin implements Exception {
66
/// contains the `response` data
7-
final T response;
7+
final Map<String, dynamic> response;
88

99
String get message;
1010

11-
LaravelException({
12-
required this.response,
13-
});
11+
LaravelException({required this.response});
1412

1513
@override
1614
String toString() => message;

lib/src/exceptions/internal_server_error.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:laravel_exception/src/exceptions/imp.dart';
22
import 'package:laravel_exception/src/models/file_stack_trace.dart';
33

44
/// * `status code of => 500
5-
class LServerException extends LaravelException<Map<String, dynamic>> {
5+
class LServerException extends LaravelException {
66
/// * Exception
77
final String? exception;
88

@@ -30,16 +30,15 @@ class LServerException extends LaravelException<Map<String, dynamic>> {
3030
response: response,
3131
);
3232

33-
factory LServerException.fromMap(Map<String, dynamic> map) {
33+
factory LServerException.parse(Map<String, dynamic> map) {
3434
return LServerException(
3535
response: map,
3636
message: map['message'],
3737
exception: map['exception'],
3838
file: map['file'],
3939
line: map['line'],
40-
trace: (map['trace'] as List?)
41-
?.map((x) => FileStackTrace.fromMap(x))
42-
.toList(),
40+
trace:
41+
(map['trace'] as List?)?.map((x) => FileStackTrace.parse(x)).toList(),
4342
);
4443
}
4544

lib/src/exceptions/not_found.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:laravel_exception/src/exceptions/imp.dart';
22

3-
class LNotFoundException extends LaravelException<Map<String, dynamic>> {
3+
class LNotFoundException extends LaravelException {
44
/// * Exception
55
final String? exception;
66

@@ -23,7 +23,7 @@ class LNotFoundException extends LaravelException<Map<String, dynamic>> {
2323
response: response,
2424
);
2525

26-
factory LNotFoundException.fromMap(Map<String, dynamic> map) {
26+
factory LNotFoundException.parse(Map<String, dynamic> map) {
2727
return LNotFoundException(
2828
response: map,
2929
message: map['message'],

lib/src/exceptions/validation_err.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import 'package:equatable/equatable.dart';
2-
31
import 'package:laravel_exception/src/exceptions/imp.dart';
42

5-
class LValidationException extends LaravelException<Map<String, dynamic>>
6-
with EquatableMixin {
3+
class LValidationException extends LaravelException {
74
final Map<String, List<String>> _errors;
85

96
LValidationException(

lib/src/extractors.dart

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/src/models/file_stack_trace.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class FileStackTrace extends Equatable {
1414
required this.type,
1515
});
1616

17-
factory FileStackTrace.fromMap(Map<String, dynamic> map) {
17+
factory FileStackTrace.parse(Map<String, dynamic> map) {
1818
return FileStackTrace(
1919
file: map['file'],
2020
line: map['line'],

pubspec.yaml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
name: laravel_exception
2-
description: parse laravel exception message to dart class with geters to make it simpler
3-
version: 0.0.1-beta
4-
publish_to: "none"
5-
homepage: https://github.com/maxzod/
2+
description: parse laravel exceptions to dart classes for easier and better handling support 422 - validation ,500 server errors , 404 not found exceptions
3+
version: 0.0.2
4+
homepage: https://github.com/maxzod/laravel_exception
65
environment:
76
sdk: ">=2.12.0 <3.0.0"
87

98
dependencies:
10-
# dio: ^4.0.0
119
equatable: ^2.0.3
12-
# json_blueprint:
13-
# git:
14-
# url: git@github.com:maxzod/json_blueprint.git
1510

1611
dev_dependencies:
1712
lints: ^1.0.1
18-
meta: ^1.3.0
1913
test: ^1.16.0

test/laraver_server_error_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ void main() {
66
test(
77
'laravel 500 response aka InternalServerError',
88
() {
9-
final exception = LServerException.fromMap(response);
9+
final exception = LServerException.parse(response);
1010
expect(exception.message, equals('Undefined variable: name'));
1111
expect(exception.exception, equals('ErrorException'));
1212
expect(

0 commit comments

Comments
 (0)