Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 855d418

Browse files
AkronimBlackbmadzaracStyleCIBot
authored
Better exceptions and response (#92)
* - Add exceptions FieldValidationException and MissingRequiredFieldException - Add Handler to extend exception handling and data formatting * Apply fixes from StyleCI --------- Co-authored-by: bmadzarac <bojan.madzarac@asseco-see.hr> Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 3d14410 commit 855d418

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+343
-207
lines changed

‎config/asseco-custom-fields.php‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,35 @@
2727
* Model bindings.
2828
*/
2929
'models' => [
30-
'custom_field' => CustomField::class,
31-
'form' => Form::class,
32-
'form_template' => FormTemplate::class,
33-
'plain_type' => PlainType::class,
34-
'relation' => Relation::class,
35-
'remote_type' => RemoteType::class,
36-
'selection_type' => SelectionType::class,
30+
'custom_field' => CustomField::class,
31+
'form' => Form::class,
32+
'form_template' => FormTemplate::class,
33+
'plain_type' => PlainType::class,
34+
'relation' => Relation::class,
35+
'remote_type' => RemoteType::class,
36+
'selection_type' => SelectionType::class,
3737
'selection_value' => SelectionValue::class,
38-
'validation' => Validation::class,
39-
'value' => Value::class,
38+
'validation' => Validation::class,
39+
'value' => Value::class,
4040
],
4141

4242
'plain_types' => [
43-
'boolean' => BooleanType::class,
43+
'boolean' => BooleanType::class,
4444
'datetime' => DateTimeType::class,
45-
'date' => DateType::class,
46-
'float' => FloatType::class,
47-
'integer' => IntegerType::class,
48-
'string' => StringType::class,
49-
'text' => TextType::class,
50-
'time' => TimeType::class,
45+
'date' => DateType::class,
46+
'float' => FloatType::class,
47+
'integer' => IntegerType::class,
48+
'string' => StringType::class,
49+
'text' => TextType::class,
50+
'time' => TimeType::class,
5151
],
5252

53-
'migrations' => [
53+
'migrations' => [
5454

5555
/**
5656
* UUIDs as primary keys.
5757
*/
58-
'uuid' => false,
58+
'uuid' => false,
5959

6060
/**
6161
* Timestamp types.
@@ -68,7 +68,7 @@
6868
* Should the package run the migrations. Set to false if you're publishing
6969
* and changing default migrations.
7070
*/
71-
'run' => true,
71+
'run' => true,
7272
],
7373

7474
/**
@@ -84,10 +84,10 @@
8484
/**
8585
* Namespace to Customizable trait.
8686
*/
87-
'trait_path' => Customizable::class,
87+
'trait_path' => Customizable::class,
8888

8989
'routes' => [
90-
'prefix' => 'api',
90+
'prefix' => 'api',
9191
'middleware' => ['api'],
9292
],
9393

‎migrations/2020_05_26_053145_create_custom_field_plain_types_table.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ protected function seedData(): void
4949
foreach ($types as $type) {
5050
if (config('asseco-custom-fields.migrations.uuid')) {
5151
$plainTypes[] = [
52-
'id' => Str::uuid(),
53-
'name' => $type,
52+
'id' => Str::uuid(),
53+
'name' => $type,
5454
'created_at' => now(),
5555
'updated_at' => now(),
5656
];
5757
} else {
5858
$plainTypes[] = [
59-
'name' => $type,
59+
'name' => $type,
6060
'created_at' => now(),
6161
'updated_at' => now(),
6262
];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Asseco\CustomFields\App\Exceptions;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class FieldValidationException extends Exception
9+
{
10+
private array $data = [];
11+
12+
public function __construct(string $message = 'Invalid form', int $code = 400, ?Throwable $previous = null, array $data = [])
13+
{
14+
$this->data = $data;
15+
parent::__construct($message, $code, $previous);
16+
}
17+
18+
public function getData(): array
19+
{
20+
return $this->data;
21+
}
22+
}

‎src/App/Exceptions/Handler.php‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Asseco\CustomFields\App\Exceptions;
4+
5+
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6+
7+
class Handler extends ExceptionHandler
8+
{
9+
/**
10+
* A list of exception types with their corresponding custom log levels.
11+
*
12+
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
13+
*/
14+
protected $levels = [
15+
//
16+
];
17+
18+
/**
19+
* A list of the exception types that are not reported.
20+
*
21+
* @var array<int, class-string<\Throwable>>
22+
*/
23+
protected $dontReport = [
24+
//
25+
];
26+
27+
/**
28+
* A list of the inputs that are never flashed to the session on validation exceptions.
29+
*
30+
* @var array<int, string>
31+
*/
32+
protected $dontFlash = [
33+
'current_password',
34+
'password',
35+
'password_confirmation',
36+
];
37+
38+
/**
39+
* Register the exception handling callbacks for the application.
40+
*
41+
* @return void
42+
*/
43+
public function register()
44+
{
45+
$this->renderable(function (MissingRequiredFieldException $e) {
46+
return response()->json([
47+
'message' => $e->getMessage(),
48+
'errors' => $e->getData(),
49+
], $e->getCode());
50+
});
51+
52+
$this->renderable(function (FieldValidationException $e) {
53+
return response()->json([
54+
'message' => $e->getMessage(),
55+
'errors' => $e->getData(),
56+
], $e->getCode());
57+
});
58+
}
59+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Asseco\CustomFields\App\Exceptions;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class MissingRequiredFieldException extends Exception
9+
{
10+
private array $data = [];
11+
12+
public function __construct(string $message = 'Invalid form', int $code = 422, ?Throwable $previous = null, array $data = [])
13+
{
14+
$this->data = $data;
15+
parent::__construct($message, $code, $previous);
16+
}
17+
18+
public function getData(): array
19+
{
20+
return $this->data;
21+
}
22+
}

‎src/App/Http/Controllers/PlainCustomFieldController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function store(PlainCustomFieldRequest $request, string $type): JsonRespo
5858

5959
$selectableData = [
6060
'selectable_type' => $typeModel,
61-
'selectable_id' => $typeModel::query()->firstOrFail('id')->id,
61+
'selectable_id' => $typeModel::query()->firstOrFail('id')->id,
6262
];
6363

6464
$customField = $this->customField::query()->create(array_merge($data, $selectableData));

‎src/App/Http/Controllers/RemoteCustomFieldController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function store(RemoteCustomFieldRequest $request): JsonResponse
6868

6969
$selectableData = [
7070
'selectable_type' => get_class($this->remoteClass),
71-
'selectable_id' => $remoteType->id,
71+
'selectable_id' => $remoteType->id,
7272
];
7373

7474
$cfData = Arr::except($data, 'remote');

‎src/App/Http/Controllers/SelectionCustomFieldController.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function store(SelectionCustomFieldRequest $request, string $type): JsonR
7474
$selectionTypeModel = $this->selectionClass;
7575
$selectionType = $selectionTypeModel::query()->create([
7676
'plain_type_id' => $plainTypeId,
77-
'multiselect' => $multiselect,
77+
'multiselect' => $multiselect,
7878
]);
7979

8080
$selectionValues = Arr::get($data, 'values', []);
@@ -89,7 +89,7 @@ public function store(SelectionCustomFieldRequest $request, string $type): JsonR
8989

9090
$selectableData = [
9191
'selectable_type' => $this->selectionClass,
92-
'selectable_id' => $selectionType->id,
92+
'selectable_id' => $selectionType->id,
9393
];
9494

9595
$cfData = Arr::except($data, ['selection', 'values']);

‎src/App/Http/Requests/CustomFieldCreateRequest.php‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ public function authorize()
2929
public function rules()
3030
{
3131
return [
32-
'name' => [
32+
'name' => [
3333
'required',
3434
'string',
3535
'regex:/^[^\s]*$/i',
3636
Rule::unique('custom_fields')->ignore($this->custom_field)->where(function ($query) {
3737
return $this->usesSoftDelete() ? $query->whereNull('deleted_at') : $query;
3838
}),
3939
],
40-
'label' => 'required|string|max:255',
41-
'placeholder' => 'nullable|string',
40+
'label' => 'required|string|max:255',
41+
'placeholder' => 'nullable|string',
4242
'selectable_type' => 'required',
43-
'selectable_id' => 'required',
44-
'model' => 'required|string',
45-
'required' => 'boolean',
46-
'hidden' => 'boolean',
47-
'validation_id' => 'nullable|exists:custom_field_validations,id',
48-
'group' => 'nullable|string',
49-
'order' => 'nullable|integer',
43+
'selectable_id' => 'required',
44+
'model' => 'required|string',
45+
'required' => 'boolean',
46+
'hidden' => 'boolean',
47+
'validation_id' => 'nullable|exists:custom_field_validations,id',
48+
'group' => 'nullable|string',
49+
'order' => 'nullable|integer',
5050
];
5151
}
5252

‎src/App/Http/Requests/CustomFieldUpdateRequest.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ public function authorize()
3535
public function rules()
3636
{
3737
$rules = [
38-
'name' => [
38+
'name' => [
3939
'required',
4040
'string',
4141
'regex:/^[^\s]*$/i',
4242
Rule::unique('custom_fields')->ignore($this->custom_field)->where(function ($query) {
4343
return $this->usesSoftDelete() ? $query->whereNull('deleted_at') : $query;
4444
}),
4545
],
46-
'label' => 'sometimes|string|max:255',
47-
'placeholder' => 'nullable|string',
48-
'required' => 'boolean',
49-
'hidden' => 'boolean',
46+
'label' => 'sometimes|string|max:255',
47+
'placeholder' => 'nullable|string',
48+
'required' => 'boolean',
49+
'hidden' => 'boolean',
5050
'validation_id' => 'nullable|exists:custom_field_validations,id',
51-
'group' => 'nullable|string',
52-
'order' => 'nullable|integer',
51+
'group' => 'nullable|string',
52+
'order' => 'nullable|integer',
5353
];
5454

5555
return Arr::except($rules, self::LOCKED_FOR_EDITING);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /