Skip to content

Commit 26ac3ad

Browse files
authored
Merge pull request #347 from aksm/ISSUE-338b
ISSUE-338: Custom error handler needs to filter out unhelpful errors/deprecations
2 parents 974bb2f + c39da32 commit 26ac3ad

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

format_strawberryfield.module

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,10 @@ function format_strawberryfield_cron() {
367367
/**
368368
* Provides custom PHP error handling when processing ajax previews.
369369
*
370-
* Converts trigger_error() to WARNING so that an exceptions can be thrown
371-
* and caught by
370+
* Throws exceptions for E_USER_WARNING and E_USER_ERROR only
371+
* to be caught by
372372
* \Drupal\format_strawberryfield\MetadataDisplayForm::ajaxPreview().
373+
* All other errors are passed back to Drupal's error handler.
373374
*
374375
* @param int $error_level
375376
* The level of the error raised.
@@ -384,11 +385,19 @@ function format_strawberryfield_cron() {
384385
* error occurred.
385386
*
386387
* @throws \ErrorException
387-
* Throw ErrorException for all errors passed from trigger_error().
388+
* Throw ErrorException for E_USER_WARNING and E_USER_ERROR only.
388389
*
389390
* @see \Drupal\format_strawberryfield\MetadataDisplayForm::ajaxPreview()
390391
*/
391392
function _format_strawberryfield_metadata_preview_error_handler($error_level, $message, $filename = NULL, $line = NULL, $context = NULL) {
392-
_drupal_error_handler(E_WARNING, $message, $filename, $line, $context);
393-
throw new ErrorException($message, $error_level, 0, $filename, $line);
393+
switch($error_level)
394+
{
395+
case E_USER_WARNING:
396+
case E_USER_ERROR:
397+
_drupal_error_handler($error_level, $message, $filename, $line, $context);
398+
throw new ErrorException($message, $error_level, 0, $filename, $line);
399+
default:
400+
_drupal_error_handler($error_level, $message, $filename, $line, $context);
401+
break;
402+
}
394403
}

src/Form/MetadataDisplayForm.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ public static function ajaxPreview($form, FormStateInterface $form_state) {
286286
$mimetype = !empty($mimetype) ? $mimetype[0]['value'] : 'text/html';
287287
$show_render_native = $form_state->getValue('render_native');
288288

289+
if ($show_render_native) {
290+
set_error_handler('_format_strawberryfield_metadata_preview_error_handler');
291+
}
292+
289293
$sbf_fields = \Drupal::service('strawberryfield.utility')
290294
->bearsStrawberryfield($preview_node);
291295

@@ -437,6 +441,9 @@ public static function ajaxPreview($form, FormStateInterface $form_state) {
437441

438442
}
439443
}
444+
if ($show_render_native) {
445+
restore_error_handler();
446+
}
440447
$response->addCommand(new OpenOffCanvasDialogCommand(t('Preview'), $output, ['width' => '50%']));
441448
}
442449
// Always refresh the Preview Element too.

0 commit comments

Comments
 (0)