Jump to content

Hooking error pages to add CORS header.


millipedia
 Share

Recommended Posts

We're building a Capacitor app that grabs data from the OpenAI API via ProcessWire (it's a fun project - I'll write it up once it's done).

For successful requests we're adding a CORS header:

    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');

and that's all good.

Occasionally though we'll get a timeout form OpenAI (or I've done something daft in the code) and PW will throw a 500 error.

Because that response doesn't have the correct headers then I can't catch the error in the app.

I've tired adding the header into

wire()->addHookBefore('Page::errorPage', function($event) {

but I'm not convinced that 500 errors even get there.

Suggestions as to how I can a header gratefully received....

 

Link to comment
Share on other sites

Well, best practice would be to make sure that your api request times out before your PHP script does (WireHttp::setTimeout / curl_setopt(CURLOPT_TIMEOUT, xx) / curl_setopt(CURLOPT_CONNECTTIMEOUT, yy) vs. max_execution_time & site_time_limit).

You can of course set the header through .htaccess instead of PHP (mod_headers must be enabled of course):

Header always set Access-Control-Allow-Origin: "*"

The keyword "always" also sends the header with redirects.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...