Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Error Handling Overhaul #1547

Merged
merged 22 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6635291
Separates error handling from success functions, updates error respon…
cayb0rg Nov 14, 2023
19476a7
Converts Response to Msg, removes react-toastify
cayb0rg Nov 15, 2023
2f5d703
Fix cache option export, fix url edge case in widget detail page, and…
cayb0rg Nov 21, 2023
ae99e85
Add error handlers to my-widgets-page and support pages
cayb0rg Nov 28, 2023
88c56d9
Replaced a no_login with no_perm
cayb0rg Nov 28, 2023
d332555
Remove unused Msg constructor and update response types
cayb0rg Nov 28, 2023
7d06ffa
Remove retry: false
cayb0rg Nov 28, 2023
3505370
Add onError to a couple apiGetUser and add missing import to profile …
cayb0rg Nov 28, 2023
74b730b
Fixes user_get 500 error, adds copy success handler to user admin pag…
cayb0rg Jan 9, 2024
c3e9f20
Merge branch 'dev/10.1.0' into issue/error-handling
cayb0rg Jan 9, 2024
7b6afe2
Merge in modified parameter to 'widgets' query
cayb0rg Jan 30, 2024
a33d5fb
Add call to successFunc in useCopyWidget
cayb0rg Jan 30, 2024
7c66bc8
Pull dev/10.1.0
cayb0rg Jan 30, 2024
c7efa1a
Merge branch 'dev/10.2.0' of github.com:ucfopen/Materia into issue/er…
cayb0rg Apr 1, 2024
aa67f33
Fixing bugs in various error states
cayb0rg Apr 2, 2024
b03d128
Merge branch 'dev/10.2.0' into issue/error-handling
clpetersonucf May 1, 2024
6646c6b
Fix return value for edit_perms_verify on no perms
cayb0rg May 8, 2024
742c1bc
Merge branch 'issue/error-handling' of github.com:cayb0rg/Materia int…
cayb0rg May 8, 2024
bdd8d66
Temporary fix for question_set_get in creator
cayb0rg May 8, 2024
fd45166
Merge branch 'dev/10.2.0' into issue/error-handling
cayb0rg May 8, 2024
069669b
Merge branch 'dev/10.2.0' into issue/error-handling
clpetersonucf May 14, 2024
f566726
Tweaked widget_instance_edit_perms_verify endpoint & handling on clie…
clpetersonucf May 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions fuel/app/classes/controller/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function before()
public function get_widget()
{
if ( ! \Materia\Perm_Manager::is_super_user() ) throw new \HttpNotFoundException;

Js::push_inline('var UPLOAD_ENABLED ="'.Config::get('materia.enable_admin_uploader').'";');
Js::push_inline('var HEROKU_WARNING ="'.Config::get('materia.heroku_admin_warning').'";');
Js::push_inline('var ACTION_LINK ="/admin/upload";');
Expand Down Expand Up @@ -78,8 +78,8 @@ public function post_upload()
}
}
}
if ($failed)

if ($failed)
{
throw new HttpServerErrorException;
}
Expand Down
53 changes: 51 additions & 2 deletions fuel/app/classes/controller/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,64 @@ public function before()
parent::before();
}

/**
* Recursively search for the status code in execution result
* @param Array
* @return Integer
*/
public function get_status($data)
{
if (is_array($data) || is_object($data))
{
foreach ($data as $key => $value)
{
if ($key === 'status')
{
return $value;
}
elseif (is_array($key) || is_object($key))
{
$result = $this->get_status($key);
if ($result !== null)
{
return $result;
}
}
}
}
}

public function post_call($version, $format, $method)
{
$input = json_decode(Input::post('data', []));

$result = $this->execute($version, $method, $input);

$status = $this->get_status($result);

if ( ! $status)
{
$status = 200;
}

$this->no_cache();
$this->response($result, 200);
$this->response($result, $status);
}

public function get_call($version, $format, $method)
{
$data = array_slice($this->request->route->method_params, 3);
$result = $this->execute($version, $method, $data);

$status = $this->get_status($result);

if ( ! $status)
{
$status = 200;
}

$this->no_cache();
$this->response($result, 200);
$this->response($result, $status);
}

protected function execute($version, $method, $args)
Expand All @@ -77,6 +118,14 @@ protected function execute($version, $method, $args)
{
Materia\Log::profile([get_class($e), get_class($api), $method, json_encode($args)], 'exception');
trace($e);
if ($e instanceof \HttpNotFoundException)
{
return Materia\Msg::not_found();
}
else
{
throw new HttpServerErrorException;
}
}
}
}
2 changes: 1 addition & 1 deletion fuel/app/classes/controller/api/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function post_widget_instance_undelete(string $inst_id)
{
if ( ! \Materia\Util_Validator::is_valid_hash($inst_id)) return Msg::invalid_input($inst_id);
if (\Service_User::verify_session() !== true) return Msg::no_login();
if ( ! ($inst = \Materia\Widget_Instance_Manager::get($inst_id, false, false, true))) return new Msg(Msg::ERROR, 'Widget instance does not exist.');
if ( ! ($inst = \Materia\Widget_Instance_Manager::get($inst_id, false, false, true))) return Msg::failure('Widget instance does not exist.');
return $inst->db_undelete();
}
}
4 changes: 2 additions & 2 deletions fuel/app/classes/controller/api/asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function post_delete($asset_id)
\Log::error('Error: In the deletion process');
\Log::error($th);

return new Msg(Msg::ERROR, 'Asset could not be deleted.');
return Msg::failure('Asset could not be deleted.');
}
}

Expand All @@ -61,7 +61,7 @@ public function post_restore($asset_id)
\Log::error('Error: In the deletion process');
\Log::error($th);

return new Msg(Msg::ERROR, 'Asset could not be restored.');
return Msg::failure('Asset could not be restored.');
}
}
}
18 changes: 10 additions & 8 deletions fuel/app/classes/controller/api/instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class Controller_Api_Instance extends Controller_Rest
*/
public function get_history()
{
if ( ! $inst_id = Input::get('inst_id')) return $this->response('Requires an inst_id parameter!', 401);
if ( ! $inst_id = Input::get('inst_id')) return $this->response(\Materia\Msg::invalid_input('Requires an inst_id parameter!'), 401);
if ( ! \Materia\Util_Validator::is_valid_hash($inst_id) ) return $this->response(\Materia\Msg::invalid_input($inst_id), 401);
if ( ! ($inst = \Materia\Widget_Instance_Manager::get($inst_id))) return $this->response('Instance not found', 404);
if ( ! \Materia\Perm_Manager::user_has_any_perm_to(\Model_User::find_current_id(), $inst_id, \Materia\Perm::INSTANCE, [\Materia\Perm::FULL])) return $this->response(\Materia\Msg::no_login(), 401);
if ( ! ($inst = \Materia\Widget_Instance_Manager::get($inst_id))) return $this->response(new \Materia\Msg('Instance not found', \Materia\Msg::ERROR), 404);
if ( ! \Materia\Perm_Manager::user_has_any_perm_to(\Model_User::find_current_id(), $inst_id, \Materia\Perm::INSTANCE, [\Materia\Perm::FULL])) return $this->response(\Materia\Msg::no_perm(), 401);

$history = $inst->get_qset_history($inst_id);

Expand All @@ -36,13 +36,15 @@ public function post_request_access()
$inst_id = Input::json('inst_id', null);
$owner_id = Input::json('owner_id', null);

if ( ! $inst_id) return $this->response('Requires an inst_id parameter', 401);
if ( ! $owner_id) return $this->response('Requires an owner_id parameter', 401);
if ( ! $inst_id) return $this->response(new \Materia\Msg('Requires an inst_id parameter', \Materia\Msg::ERROR), 401);

if ( ! \Model_User::find_by_id($owner_id)) return $this->response('Owner not found', 404);
if ( ! ($inst = \Materia\Widget_Instance_Manager::get($inst_id))) return $this->response('Instance not found', 404);
if ( ! $owner_id) return $this->response(new \Materia\Msg('Requires an owner_id parameter', \Materia\Msg::ERROR), 401);

if ( ! Materia\Perm_Manager::user_has_any_perm_to($owner_id, $inst_id, Materia\Perm::INSTANCE, [Materia\Perm::FULL, Materia\Perm::VISIBLE])) return $this->response('Owner does not own instance', 404);
if ( ! \Model_User::find_by_id($owner_id)) return $this->response(new \Materia\Msg('Owner not found', \Materia\Msg::ERROR), 404);

if ( ! ($inst = \Materia\Widget_Instance_Manager::get($inst_id))) return $this->response(new \Materia\Msg('Instance not found', \Materia\Msg::ERROR), 404);

if ( ! Materia\Perm_Manager::user_has_any_perm_to($owner_id, $inst_id, Materia\Perm::INSTANCE, [Materia\Perm::FULL, Materia\Perm::VISIBLE])) return $this->response(new \Materia\Msg('Owner does not own instance', \Materia\Msg::ERROR), 404);

if ( ! \Materia\Util_Validator::is_valid_hash($inst_id) ) return $this->response(\Materia\Msg::invalid_input($inst_id), 401);

Expand Down
8 changes: 4 additions & 4 deletions fuel/app/classes/controller/api/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function get_activity()

public function post_settings()
{
if (\Service_User::verify_session() !== true) return $this->response('Not logged in', 401);
if (\Service_User::verify_session() !== true) return $this->response(\Materia\Msg::no_login(), 401);

$success = false;
$set_meta = [
Expand All @@ -42,9 +42,9 @@ public function post_settings()

public function post_roles()
{
if (\Service_User::verify_session() !== true) return $this->response('Not logged in', 401);
if (\Service_User::verify_session() !== true) return $this->response(\Materia\Msg::no_login(), 401);
// this endpoint is only available to superusers!
if ( ! \Materia\Perm_Manager::is_super_user()) return $this->response('Not authorized', 403);
if ( ! \Materia\Perm_Manager::is_super_user()) return $this->response(\Materia\Msg::no_perm(), 403);

$success = false;
$user_id = Input::json('id', null);
Expand All @@ -53,7 +53,7 @@ public function post_roles()
'support_user' => Input::json('support_user', false)
];

if ( ! $user_id) return $this->response('User ID not provided', 401);
if ( ! $user_id) return $this->response(\Materia\Msg::invalid_input('User ID not provided'), 401);

$current_roles = \Materia\Perm_Manager::get_user_roles($user_id);
$current_roles_condensed = array_map( fn($r) => $r->name, $current_roles);
Expand Down
2 changes: 1 addition & 1 deletion fuel/app/classes/controller/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function action_upload()
$name = Input::post('name', 'New Asset');
$asset = Widget_Asset_Manager::new_asset_from_file($name, $file_info);

if ( ! isset($asset->id))
if ( ! $asset || ! isset($asset->id))
{
// error
trace('Unable to create asset');
Expand Down
Loading
Loading