Catch script output/warnings

This commit is contained in:
Jay Trees 2022-03-11 15:57:01 +01:00
parent 6ccd38cc9b
commit 05124129b5
2 changed files with 31 additions and 4 deletions

View file

@ -11,6 +11,8 @@ use wishthis\{User, Wish, EmbedCache};
$api = true;
$response = array();
ob_start();
require '../../index.php';
switch ($_SERVER['REQUEST_METHOD']) {
@ -34,7 +36,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
$exists = $cache->exists() ? 'true' : 'false';
$response = array(
'info' => $info
'info' => $info,
);
}
break;
@ -117,6 +119,8 @@ switch ($_SERVER['REQUEST_METHOD']) {
break;
}
$response['warning'] = ob_get_clean();
header('Content-type: application/json; charset=utf-8');
echo json_encode($response);
die();

View file

@ -125,14 +125,19 @@ function handleFetchResponse(response) {
var isJSON = response.headers.get('content-type')?.includes('application/json');
if (isText) {
return response.text()
.then(function(text) {
return response.text().then(function(text) {
if (text.toLowerCase().includes('error') || text.toLowerCase().includes('exception')) {
showError(text);
}
})
} else if (isJSON) {
return response.json();
return response.json().then(function(json) {
if (json.warning) {
showWarning(json.warning)
}
return json;
});
}
}
@ -159,3 +164,21 @@ function showError(error) {
autoShow: true
});
}
function showWarning(warning) {
warning = warning.replace('<br />', '');
$('body')
.modal({
title : 'Warning',
content : warning,
class : '',
actions : [
{
text: 'Understood',
class: 'primary'
}
],
autoShow: true,
});
}