millipedia Posted November 14, 2023 Share Posted November 14, 2023 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 More sharing options...
BitPoet Posted November 14, 2023 Share Posted November 14, 2023 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. 1 1 Link to comment Share on other sites More sharing options...
millipedia Posted November 15, 2023 Author Share Posted November 15, 2023 Great - that's really useful. I didn't know about the 'always' condition in .htaccess (even though it's all over the place in the usual .htaccess file ....). I think a combination of that and catching my errors earlier should do me. Thanks for your help. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now