Browse Source

better error handling and error messages

9.x-1.0
ppound 3 years ago
parent
commit
cbaa7db200
  1. 6
      includes/relais.inc
  2. 14
      src/Form/RoblibIllLoanForm.php

6
includes/relais.inc

@ -123,7 +123,13 @@ function upei_roblib_ill_authenticate($barcode, $surname) {
$response = \Drupal::httpClient()->post($url, $options);
}
catch (Exception $e) {
$human_readable = '';
if($e->getCode() == '401') {
$human_readable = t('There may have been an issue with your Campus ID, '
. 'Please verify it is correct. ');
}
$response_data['Problem']['Message'] = $e->getMessage();
$response_data['Problem']['HR_Message'] = upei_roblib_ill_build_err_msg($human_readable);
return $response_data;
}

14
src/Form/RoblibIllLoanForm.php

@ -48,8 +48,10 @@ class RoblibIllLoanForm extends FormBase {
$aid = upei_roblib_ill_authenticate($campus_id, $values['Surname']);
if (is_array($aid) && isset($aid['Problem']['Message'])) {
if (!empty($aid['Problem']['HR_Message'])) {
$form_state->setErrorByName('campus_id', $aid['Problem']['HR_Message']);
}
$form_state->setErrorByName('Surname', $aid['Problem']['Message']);
$form_state->setErrorByName('campus_id', '');
// Log invalid requests, we need to build the full array here to log it, even though we won't send it yet as auth has failed.
//$form_state->set('upei_roblib_ill_auth_form_storage') = $form_state->getValues();
module_load_include('inc', 'upei_roblib_ill', 'includes/db');
@ -61,9 +63,14 @@ class RoblibIllLoanForm extends FormBase {
}
}
$trigger = $form_state->getTriggeringElement();
if (empty($form_state->get('step')) && $trigger['#value'] != 'Lookup DOI' && empty($form_state->getValue('Title'))) {
if($trigger['#value'] != 'Lookup DOI' && empty($form_state->get('step'))) {
if(empty($form_state->getValue('Title'))){
$form_state->setErrorByName('Title', t('Journal/Book Title is required.'));
}
if(empty($form_state->getValue('Genre'))) {
$form_state->setErrorByName('Title', t('Item Type is required.'));
}
}
}
/**
@ -148,7 +155,8 @@ class RoblibIllLoanForm extends FormBase {
'chapter' => t('Book Chapter'),
'article' => t('Journal Article'),
],
'#required' => TRUE,
'#default_value' => 'article',
'#required' => FALSE,
];
if (!empty($type)) {
$form['Genre']['#default_value'] = $type;

Loading…
Cancel
Save