12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- use Symfony\Component\HttpFoundation\Response;
- class Errors
- {
- /**
- * Generate Response from error.
- * @param string $message Contains message.
- * @param int $status Contains status code.
- * @return Response Return object Response.
- */
- function error($message = "Error", $status = 500)
- {
- if ($message != null && !is_string($message) && $message instanceof Throwable) {
- return new Response(json_encode(["code" => $message->getCode(), "message" => $message->getMessage(), "trace" => $message->getTraceAsString()]), $status);
- } else {
- return new Response($message, $status);
- }
- }
- /**
- * @param string $property Contains the name of property
- * @param string $type Contains type of property
- * @return Error Throws errors.
- */
- function errorRequired($property, $type)
- {
- return new Error("Parameter " . $property . " is required and type is " . $type . ".", 100000);
- }
- /**
- * @param string $property Contains the name of property
- * @return Error Throws errors.
- */
- function errorString($property)
- {
- return new Error("Incorrect " . $property . ". Only support string.", 100001);
- }
- /**
- * @param string $property Contains the name of property
- * @return Error Throws errors.
- */
- function errorInteger($property)
- {
- return new Error("Incorrect " . $property . ". Only support integer.", 100002);
- }
- }
|