Код в PHP выкидывает Exception
if ( ! $table_name ) {
throw new Exception('Тип поста не определен. Не могу понять, с какой таблицей БД работать.');
}
Обработка исключения в PHP
try {
$result = $offer->get_offers_view_by_ids( $request->otherPostIds, $request->locale );
} catch ( \Exception $e ) {
echo json_encode( [ 'error' => true, 'message' => $e->getMessage() ] );
die();
}
Отображаю сообщение об ошибке в JS
const urlRequest = 'modules/offer/offer-ajax.php';
const sendAjaxRequest = new SendAjaxRequest(urlRequest, 'json');
sendAjaxRequest.getResponse(data).then(r => {
if ('error' in r && 'message' in r) {
console.error(r.message);
return;
}
...
}