Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware order not work #478

Open
IgordeOliveira opened this issue Jan 29, 2025 · 1 comment
Open

Middleware order not work #478

IgordeOliveira opened this issue Jan 29, 2025 · 1 comment

Comments

@IgordeOliveira
Copy link

IgordeOliveira commented Jan 29, 2025

I developed a middleware to log all my requests and responses:

<?php

namespace App\Http\Integrations;

use Illuminate\Support\Facades\Log;
use Saloon\Contracts\ResponseMiddleware;
use Saloon\Http\Response;

class LoggingMiddleware implements ResponseMiddleware
{
    public function __construct(private readonly string $connector)
    {
    }

    public function __invoke(Response $response, ): void
    {
        // Log the request
        Log::info($this->connector . ' API Call', [
            'request' => [
                'method' => $response->getRequest()->getMethod()->value,
                'url' => $response->getRequest()->resolveEndpoint(),
                'headers' => $response->getRequest()->headers()->all(),
                'query' => $response->getRequest()->query()->all(),
                'body' => $response->getPendingRequest()->body()->all(),
            ],
            'response' => [
                'status' => $response->status(),
                'headers' => $response->headers()->all(),
                'body' => $response->body(),
            ],
        ]);
    }
}

Include in the construct

       $this->middleware()->onResponse(new LoggingMiddleware('Name'),order: PipeOrder::FIRST);

But if I use the Rate Limit plugin and an exception is thrown by the plugin, my middleware is not called.

@craigpotter
Copy link
Contributor

Hi @IgordeOliveira
I have just release the initial version of Barstool which should solve all your problems although it will save the logs to a database.

Would be interested to hear your thoughts and to see if it works with the RateLimit plugin, I don't see why not.

Your issue is that the onResponse middleware doesn't get run when an exception is thrown. You need to use the ->onFatal() (I believe, I'm on my phone so can't check).
Maybe check the implementation of Barstool if you want to carry on with your solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants