Skip to content

Commit

Permalink
Add error constant to Http Response and refactor usage
Browse files Browse the repository at this point in the history
This commit refactors the hard-coded numbers representing success and error status in HTTP Responses.
A new constant, ERROR, has been created in the
  • Loading branch information
AuroraYolo committed Aug 24, 2023
1 parent 863c70c commit fee8029
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/Kernel/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Response
{
public const OK = 0;

public const ERROR = -1;

protected ResponseInterface $response;

/**
Expand All @@ -40,7 +42,7 @@ public function __construct(protected ContainerInterface $container)
public function success(mixed $data = []): PsrResponseInterface
{
return $this->response->json([
'code' => 0,
'code' => self::OK,
'data' => $data,
]);
}
Expand All @@ -52,7 +54,7 @@ public function fail(ErrorCodeInterface|string|Exception $error): PsrResponseInt
}

return $this->response->json([
'code' => -1,
'code' => self::ERROR,
'message' => (string) $error,
]);
}
Expand Down

0 comments on commit fee8029

Please sign in to comment.