# Error logs API (user)

Authenticated users can list **their own** persisted error history (for example failures during Stripe Connect onboarding). Logs are stored in MongoDB collection `error_logs` and scoped strictly to the current user.

---

## List my error logs

**Method & path:** `GET /api/v1/users/error-logs`

**Route registration:** `UserRoutes.route("/error-logs").get(UserErrorLogController.listMyErrorLogs)` inside the `/users` group with **user JWT middleware** (same as profile, Stripe onboarding, etc.).

### Authentication

| Header | Value |
|--------|--------|
| `Authorization` | `Bearer <access_token>` |

Other token locations supported by your app middleware (if any) follow the same rules as other `/api/v1/users/*` routes.

### Query parameters

| Parameter | Type | Default | Max | Description |
|-----------|------|---------|-----|-------------|
| `page` | integer | `1` | — | Page number (minimum `1`). |
| `limit` | integer | `20` | `100` | Page size. |

Invalid or missing values fall back to defaults; `limit` is capped at 100.

### Success response

**HTTP status:** `200`

**Body shape** (via `res.success`):

```json
{
  "status": true,
  "message": "Error logs fetched successfully",
  "data": {
    "items": [],
    "total": 0,
    "page": 1,
    "limit": 20,
    "totalPages": 0
  }
}
```

- **`items`:** Newest first (`createdAt` descending). Each element is one error log document (see below).
- **`total`:** Total matching documents for this user (all pages).
- **`page` / `limit`:** Echo of the effective pagination used.
- **`totalPages`:** `ceil(total / limit)`, or `0` when `total` is `0`.

### Error log item (`items[]`)

Fields returned (Mongoose `__v` is omitted):

| Field | Type | Description |
|-------|------|-------------|
| `_id` | string | Log document id. |
| `user` | string (ObjectId) | Owner user id (always the authenticated user for this API). |
| `message` | string | Primary error message. |
| `name` | string | Error name when available (e.g. `Error`, `TypeError`). |
| `stack` | string | Stack trace when stored (may be truncated at write time). |
| `statusCode` | number | HTTP-style status associated with the failure, if set. |
| `method` | string | HTTP method of the request when logged from an HTTP handler. |
| `path` | string | Request path when logged from an HTTP handler. |
| `source` | string | One of: `http`, `socket`, `cron`, `worker`, `system`, `unknown`. |
| `metadata` | object | Extra context (e.g. `{ "feature": "stripe_onboarding" }`). |
| `createdAt` | string (ISO date) | When the log was created. |
| `updatedAt` | string (ISO date) | When the document was last updated. |

### Example request

```http
GET /api/v1/users/error-logs?page=1&limit=10 HTTP/1.1
Host: <your-api-host>
Authorization: Bearer <user_access_token>
```

### Example `items` entry (Stripe onboarding failure)

```json
{
  "_id": "674a1b2c3d4e5f6789abcdef",
  "user": "674a0b1c2d3e4f56789abcde",
  "message": "No such account: 'acct_xxx'",
  "name": "Error",
  "stack": "Error: No such account...",
  "statusCode": 500,
  "method": "POST",
  "path": "/api/v1/users/stripe/onboarding",
  "source": "http",
  "metadata": {
    "feature": "stripe_onboarding"
  },
  "createdAt": "2026-04-22T12:00:00.000Z",
  "updatedAt": "2026-04-22T12:00:00.000Z"
}
```

### Error responses

| HTTP | When |
|------|------|
| `401` | Missing/invalid token or user not resolved (`Unauthorized`). |
| `500` | Server failure while reading logs. |

Error body uses the same `{ "status": false, "message": "...", "data": null }` pattern as other user APIs.

---

## Related: where logs are written

Server code calls `saveErrorLog` from `src/services/errorLog.js` with the **current user id** when persisting (for example Stripe Connect onboarding errors in `StripeController.createStripeConnectAccount`). Additional features can reuse `saveErrorLog` with a `metadata` discriminator (e.g. `feature`) so the app can group or label rows in the UI.

---

## Privacy note

This endpoint returns **only** logs whose `user` field matches the authenticated user. There is **no** query parameter to read another user’s logs.
