Skip to content

Commit

Permalink
Log errors during resource update operation (typically these happen a… (
Browse files Browse the repository at this point in the history
#67)

* Log errors during resource update operation (typically these happen at the resource storage layer)

* Apply fixes from StyleCI (#68)
  • Loading branch information
specialtactics authored Jan 24, 2024
1 parent a7c4a2c commit 9c2ed2a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function APIUser()
*
* @deprecated Use Helpers.php
*
* @param $array
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
Expand All @@ -31,7 +31,7 @@ function camel_case_array_keys($array, $levels = null)
*
* @deprecated Use Helpers.php
*
* @param $array
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
Expand Down
4 changes: 2 additions & 2 deletions src/APIHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class APIHelpers
/**
* Recursively camel-case an array's keys
*
* @param $array
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
Expand Down Expand Up @@ -42,7 +42,7 @@ public static function camelCaseArrayKeys($array, $levels = null)
/**
* Recursively snake-case an array's keys
*
* @param $array
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Controllers/Features/RestfulControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function shouldTransform()
* Useful for adding error info to internal request responses before returning them
*
* @param \Dingo\Api\Http\Response $response
* @param $message
* @param $message
* @return \Dingo\Api\Http\Response
*/
protected function prependResponseMessage($response, $message)
Expand All @@ -122,8 +122,8 @@ protected function prependResponseMessage($response, $message)
/**
* Try to find the relation name of the child model in the parent model
*
* @param $parent Object Parent model instance
* @param $child string Child model name
* @param $parent Object Parent model instance
* @param $child string Child model name
* @return null|string
*/
protected function getChildRelationNameForParent($parent, $child)
Expand Down
10 changes: 5 additions & 5 deletions src/Http/Controllers/RestfulChildController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class RestfulChildController extends BaseRestfulController
* @var array
*/
public $parentAbilitiesRequired = [
'create' => 'update',
'view' => 'view',
'viewAll' => 'view',
'update' => 'own',
'delete' => 'own',
'create' => 'update',
'view' => 'view',
'viewAll' => 'view',
'update' => 'own',
'delete' => 'own',
];

/**
Expand Down
15 changes: 11 additions & 4 deletions src/Services/RestfulService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
Expand Down Expand Up @@ -51,8 +52,8 @@ public function setModel($model)
/**
* Deletes resources of the given model and uuid(s)
*
* @param $model string Model class name
* @param $uuid string|array The UUID(s) of the models to remove
* @param $model string Model class name
* @param $uuid string|array The UUID(s) of the models to remove
* @return mixed
*/
public function delete($model, $uuid)
Expand Down Expand Up @@ -82,6 +83,9 @@ public function patch($model, array $data)
try {
$resource = $model->update($data);
} catch (\Exception $e) {
// Log this as an unexpected exception, useful if API debug is off
Log::error('Error updating resource ' . get_class($model) . ': ' . $e->getMessage());

// Check for QueryException - if so, we may want to display a more meaningful message, or help with
// development debugging
if ($e instanceof QueryException) {
Expand All @@ -108,15 +112,18 @@ public function patch($model, array $data)
/**
* Create model in the database
*
* @param $model
* @param $data
* @param $model
* @param $data
* @return mixed
*/
public function persistResource(RestfulModel $resource)
{
try {
$resource->save();
} catch (\Exception $e) {
// Log this as an unexpected exception, useful if API debug is off
Log::error('Error persisting resource ' . get_class($resource) . ': ' . $e->getMessage());

// Check for QueryException - if so, we may want to display a more meaningful message, or help with
// development debugging
if ($e instanceof QueryException) {
Expand Down

0 comments on commit 9c2ed2a

Please sign in to comment.