# [Usage](../../../usage.md) > Response helper

Use `ResponseHelper` when a controller or a component `action` closure must answer with a redirect or JSON in the framework convention: `success` / `error` (+ optional `errorlog`) keys that [Session messages](../frontend/session_messages.md) and SweetAlert2 already understand. For plain `back()->with()` calls you can write them by hand; reach for this helper when handling exceptions with a log reference.

---

## Method

### Redirects (for pages and form actions)

- **`ResponseHelper::returnBackSuccess($message = null)`**  
  `back()->with('success', $message)`.

- **`ResponseHelper::returnBackError($message = null, $e = null)`**  
  `back()->with(['error' => $message ?? generic, 'errorlog' => $errorId])` when `$e` is given.

- **`ResponseHelper::returnBackGenericError($e = null)`**  
  Same with the generic `kfw::ui.generic_error` message, always with `errorlog` when `$e` is given.

### JSON (for AJAX / API-style actions)

- **`ResponseHelper::returnJsonSuccess($message = null)`**  
  `{'success': $message}`.

- **`ResponseHelper::returnJsonError($message = null, $e = null)`**  
  `{'error': $message ?? generic, 'errorlog': $errorId}` when `$e` is given.

- **`ResponseHelper::returnJsonGenericError($e = null)`**  
  Same with the generic message.

#### What it does with `$e`

When an exception/throwable is passed, it is logged as `KFW error <uuid>` and the uuid is returned as `errorlog`. Show `errorlog` to the user (session-messages already do) and grep it in `laravel.log` — the real message never leaks to the browser.

---

## Output

- Redirects: `RedirectResponse` with flashed `success` / `error` / `errorlog`.
- JSON: `JsonResponse` with `success` or `error` (+ `errorlog`) keys.

---

## Example

```php
use KFramework\Helpers\ResponseHelper;

// Page controller:
public function store(Request $request)
{
    try {
        Order::create($request->validated());
    } catch (\Throwable $e) {
        return ResponseHelper::returnBackGenericError($e);
    }

    return ResponseHelper::returnBackSuccess('Order created.');
}

// Component action closure (Button / Form setAction):
Button::make('Retry')->setAction(function () {
    try {
        // ... work ...
    } catch (\Throwable $e) {
        return ResponseHelper::returnJsonGenericError($e);
    }

    return ResponseHelper::returnJsonSuccess('Done.');
});
```

Display the log id from the frontend (already automatic with `<x-kfw::session-messages />`):

```php
return back()->with(['error' => 'Order failed.', 'errorlog' => $errorId]);
```

## See also

- [Usage](../../../usage.md)
- [Session messages](../frontend/session_messages.md) — timers, `errorlog` display, personalization
- [Action](../../components/shared_attributes/action.md) — where to return these responses in components
