Skip to content

Commit

Permalink
Improve API responses for invalid/missing things
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyker committed May 18, 2022
1 parent 2b6036c commit 380d511
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
16 changes: 11 additions & 5 deletions app/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ public function getModpackIndex()

public function getModpack($slug)
{
return response()->json($this->fetchModpack($slug));
$response = $this->fetchModpack($slug);

if (! $response) {
return response()->json(['error' => 'Modpack does not exist'], 404);
}

return response()->json($response);
}

public function getModpackBuild($modpackSlug, $buildName)
Expand Down Expand Up @@ -138,7 +144,7 @@ public function getMod($modSlug = null, $version = null)
$modVersion = $mod->versions()->where('version', $version)->first();

if (! $modVersion) {
return response()->json(['error' => 'Mod version does not exist']);
return response()->json(['error' => 'Mod version does not exist'], 404);
}

$response = $modVersion->only([
Expand All @@ -155,13 +161,13 @@ public function getMod($modSlug = null, $version = null)
public function getVerify($key = null)
{
if (! $key) {
return response()->json(['error' => 'No API key provided.']);
return response()->json(['error' => 'No API key provided.'], 400);
}

$key = Key::where('api_key', $key)->first();

if (! $key) {
return response()->json(['error' => 'Invalid key provided.']);
return response()->json(['error' => 'Invalid key provided.'], 403);
}

return response()->json([
Expand Down Expand Up @@ -222,7 +228,7 @@ private function fetchModpack($slug)
}

if (! $modpack) {
return ['error' => 'Modpack does not exist'];
return null;
}

return $modpack->toApiResponse($this->client, $this->key);
Expand Down
21 changes: 18 additions & 3 deletions tests/Unit/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function test_mod()
public function test_invalid_modpack()
{
$response = $this->get('api/modpack/bob');
$response->assertOk();
$response->assertJsonStructure(['error']);
$response->assertNotFound();
$response->assertJson(['error' => 'Modpack does not exist']);
}

public function test_modpack_slug()
Expand Down Expand Up @@ -76,7 +76,7 @@ public function test_invalid_mod()
{
$response = $this->get('api/mod/bob');
$response->assertNotFound();
$response->assertJsonStructure(['error']);
$response->assertJson(['error' => 'Mod does not exist']);
}

public function test_mod_slug()
Expand Down Expand Up @@ -121,4 +121,19 @@ public function test_mod_version()
'url',
]);
}

public function test_modversion_with_invalid_mod()
{
$response = $this->get('api/mod/foo/bar');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod does not exist']);
}

public function test_invalid_modversion()
{
$mod = Mod::find(1);
$response = $this->get('api/mod/'.$mod->name.'/invalid');
$response->assertNotFound();
$response->assertJson(['error' => 'Mod version does not exist']);
}
}

0 comments on commit 380d511

Please sign in to comment.