eidellev / inertiajs-adonisjs

279 stars 17 forks source link

Intercept All Inertia requests must receive a valid Inertia response #36

Closed anishghimire862 closed 3 years ago

anishghimire862 commented 3 years ago

Hello,

Is there a way I can intercept All Inertia requests must receive a valid Inertia response errors and perform some tasks? Actually I am trying to log errors to some crash reporting platform. I am able to catch and log errors with status 400 and 500 but since All Inertia requests must receive a valid Inertia response, has a status code of 200-300 I am not able to capture such errors.

Any help will be appreciated.

Thank you.

eidellev commented 3 years ago

Hi @anishghimire862 ! That's an interesting question. I guess it depends on the types of scenarios you're trying to capture. If it's validation errors, you will get a 422 response. Other types of errors like 404 or 500 you can catch in Exceptions/Handler.ts and log them there.

anishghimire862 commented 3 years ago

Thank you for your quick response.

Actually, I am not trying to catch validation errors since they are handled separately. And I have handled 404 and 500 errors from Handler.ts as you've said.

Well, I am trying to catch the error that comes in a pop up stating that 'All Inertia requests must receive a valid Inertia response, however a plain JSON response was received'. Since this error has a status code 200 I am unable to catch it from Handler.ts.

eidellev commented 3 years ago

I'm not familiar with this popup. Can you share a screenshot and some code that will help me reproduce it?

anishghimire862 commented 3 years ago

This is the popup I am referring to. image

Here is the request information which has 200 status code: image

What I have done is:

If a condition is met I redirect back the user to the same page using redirectBack and in other cases, I'm just returning an object.

eidellev commented 3 years ago

thanks. can you share your controller as well?

anishghimire862 commented 3 years ago

I think it is neither Inertia nor InertiaAdonis's fault. Inertia is just getting a non-json response and doesn't know what to do with it. So my controller should probably do a better job of returning the right response based on the situation.

If I am able to repro it I will write a controller and share it here,

Thank you for your time.

eidellev commented 3 years ago

Thanks! I'm closing this issue for now, but please keep me posted if you run into any other issues.