Thread overview
vibe.d: Finding out if currently in webinterface request
Aug 10
Timoses
Aug 10
Seb
August 09
I already posted this in the vibe.d forums (https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here:

Is there a way to find out, if we are currently in a webinterface request?

My usecase is the following:

I want to implement a logger, which in addition to the actual log message also logs information about the request which is currently being handled (if any). For this I would like to call request(), which gives me the current HTTPServerRequest. However, this results in an AssertError, whenever logging appears outside of a webinterface request (so basically immediately after starting the application, because vibe.d logs some things automatically). So I would need to check, if we are currently processing a webinterface request.

Another usecase which would be interesting to me and which has the exact same problem is the following:

Suppose in my received request, there is an HTTP header X-Correlation-Id: <uuid>. Now whenever during processing of this request I start an HTTP request to some other service (e.g. via requestHTTP()), I also want to include this header in the new request I send. Of course, you could implement this by always passing the header manually, but I'd prefer to implement this using a generalized wrapper around requestHTTP(). But then in this wrapper, I would also need to call request(), which makes it impossible to call it from outside of webinterface request (e.g. it could be triggered by a timer, or something like this). But it would be nice, to be able to just use a single function for all my requests (to keep things uniform).
August 10
On Thursday, 9 August 2018 at 21:59:24 UTC, Johannes Loher wrote:
> I already posted this in the vibe.d forums (https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here:
>
> [...]

Do you have some code segments boiled down to the problem?

Are you using vibe.core.log?
August 10
On Friday, 10 August 2018 at 09:33:34 UTC, Timoses wrote:
> On Thursday, 9 August 2018 at 21:59:24 UTC, Johannes Loher wrote:
>> I already posted this in the vibe.d forums (https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here:
>>
>> [...]
>
> Do you have some code segments boiled down to the problem?
>
> Are you using vibe.core.log?

Yes, I am using vibe.core.log, but as mentioned, I want to implement my own logger. The gist of what I am trying to do is this:

https://run.dlang.io/is/7qpJ6J

This actually works the way it is, but it involves catch Throwable (actually AssertError would be enough) and this is bad. I would like to find a different solution.
August 10
On Friday, 10 August 2018 at 18:23:28 UTC, Johannes Loher wrote:
> On Friday, 10 August 2018 at 09:33:34 UTC, Timoses wrote:
>> On Thursday, 9 August 2018 at 21:59:24 UTC, Johannes Loher wrote:
>>> I already posted this in the vibe.d forums (https://forum.rejectedsoftware.com/groups/rejectedsoftware.vibed/thread/58891/), but it seems, there is not a lot of activity over there, so I am cross posting this here:
>>>
>>> [...]
>>
>> Do you have some code segments boiled down to the problem?
>>
>> Are you using vibe.core.log?
>
> Yes, I am using vibe.core.log, but as mentioned, I want to implement my own logger. The gist of what I am trying to do is this:
>
> https://run.dlang.io/is/7qpJ6J
>
> This actually works the way it is, but it involves catch Throwable (actually AssertError would be enough) and this is bad. I would like to find a different solution.

You hit the assert of getRequestContext here: https://github.com/vibe-d/vibe.d/blob/a9589d955f10bd076a67d47ace0c78cfd3aa8246/web/vibe/web/web.d#L871

However, I don't know of a way yet to check whether one is currently in a request context from outside of vibe.web.web (adding a method to do so to vibe.web.web which checks `s_requestContext.req !is null` would probably help.)