Errors.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. use Symfony\Component\HttpFoundation\Response;
  3. class Errors
  4. {
  5. /**
  6. * Generate Response from error.
  7. * @param string $message Contains message.
  8. * @param int $status Contains status code.
  9. * @return Response Return object Response.
  10. */
  11. function error($message = "Error", $status = 500)
  12. {
  13. if ($message != null && !is_string($message) && $message instanceof Throwable) {
  14. return new Response(json_encode(["code" => $message->getCode(), "message" => $message->getMessage(), "trace" => $message->getTraceAsString()]), $status);
  15. } else {
  16. return new Response($message, $status);
  17. }
  18. }
  19. /**
  20. * @param string $property Contains the name of property
  21. * @param string $type Contains type of property
  22. * @return Error Throws errors.
  23. */
  24. function errorRequired($property, $type)
  25. {
  26. return new Error("Parameter " . $property . " is required and type is " . $type . ".", 100000);
  27. }
  28. /**
  29. * @param string $property Contains the name of property
  30. * @return Error Throws errors.
  31. */
  32. function errorString($property)
  33. {
  34. return new Error("Incorrect " . $property . ". Only support string.", 100001);
  35. }
  36. /**
  37. * @param string $property Contains the name of property
  38. * @return Error Throws errors.
  39. */
  40. function errorInteger($property)
  41. {
  42. return new Error("Incorrect " . $property . ". Only support integer.", 100002);
  43. }
  44. }