Skip to content

Instantly share code, notes, and snippets.

@amorkovin
Last active June 15, 2021 12:46
Show Gist options
  • Save amorkovin/72228c94fd9076416b7f2f442bb75ebf to your computer and use it in GitHub Desktop.
Save amorkovin/72228c94fd9076416b7f2f442bb75ebf to your computer and use it in GitHub Desktop.
Execption php и его обработка в js

Exception в PHP и его трансляция в JS

Код в 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;
		}

		...
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment