---
# System prepended metadata

title: List API
tags: [' Ciputra', ' API', NBS]

---

---
title: List API
tags: NBS, Ciputra, API
---
Success Response
===
```json=
{
    "data": {
        "user":  [{
            "id": 111
            "name": "John Doe",
        }],
        "etc": {}
    }
}
```
Error Response
===
```json=
{
    "code": 422,
    "message": "The given data was invalid",
    "timestamp": "2020-01-10 07:42:06",
    "_trace": { // optional, bisa di kirim atau tidak
        "validations": {
            "code": 422,
            "message": "Missing/Invalid Input Parameter.",
            "trace": {
                "user_id": [
                    "The user id must be an integer."
                ]
            }
        }
    }
}
```

Code
===

```php=
private function errorResponse(Response $response)
    {
        $error = [
            'code' => '',
            'message' => '',
            'timestamp' => '',
        ];

        $errorMapping = config("errors." . get_class($response->exception));

        $error['code'] = (string)($errorMapping['code'] ?? Response::HTTP_INTERNAL_SERVER_ERROR);
        $error['message'] = ($response->exception->getMessage()) ? $response->exception->getMessage() : ($errorMapping['message'] ?? 'Internal Server Error');
        $error['timestamp'] = date('Y-m-d H:i:s');

        if (config('app.debug') == 'true') {
            $error['_trace']['exception'] = get_class($response->exception);
            $error['_trace']['stack'] = $response->exception->getFile() . ' - ' . $response->exception->getLine();
            $error['_trace']['message'] = $response->exception->getMessage();

            //handle response for validation exception
            if (isset($response->exception->status)
                && $response->exception->status
                === Response::HTTP_UNPROCESSABLE_ENTITY) {
                $error['code'] = Response::HTTP_UNPROCESSABLE_ENTITY;
                $error['_trace']['validations'] = $response->exception->getResponse()->original;
            }
        }

        if (isset($errorMapping['status'])) {
            $response->setStatusCode($errorMapping['status']);
        }
        $response->headers->set('Content-Type', 'application/json');
        $response->setContent(json_encode($error));

        return $response;
    }
```
