Skip to content

Instantly share code, notes, and snippets.

@purwandi
Created June 2, 2016 14:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save purwandi/b7a56be74f840f6f87e77058de9b246e to your computer and use it in GitHub Desktop.
Save purwandi/b7a56be74f840f6f87e77058de9b246e to your computer and use it in GitHub Desktop.
Custom error format response Lumen Framework
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController
{
public function __construct()
{
$this->customErrorFormat();
}
/**
* Custom error response format
*
* @return Illuminate\Http\Response
*/
private function customErrorFormat()
{
static::$errorFormatter = function ($validator) {
$arr = [];
foreach ($validator->errors()->toArray() as $key => $value) {
$arr[$key] = $value[0];
}
return [
'errors' => $arr,
'meta' => [
'code' => 422,
'message' => 'VALIDATION_FAILED',
],
];
};
}
}
@Dylan-Buth
Copy link

👍 Thanks for this. Always nice when copy and pasting someone else's code solves your problem.

@nwaweru
Copy link

nwaweru commented Oct 8, 2019

Thank you for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment