<commit_before>'use strict';

import Log        from '../../shared/util/Log';
import {AC}       from '../actions';
import HTTPStatus from 'http-status';

const log = new Log('unexpectedError');

/**
 * The handleUnexpectedError() function provides a common utility to
 * consistently report/log client-side errors in a standard way.
 * 
 *  - The supplied err is appropriately logged.
 *
 *  - A redux action is created/returned that will provide appropriate
 *    communication to the user in a standardized way (with an
 *    optional details link to glean additional 'technical'
 *    information).
 *
 * NOTE: The returned action MUST BE dispatched (through the redux store)
 *       in order for the user to visualize the error condition.
 *
 * @param {Error} err the error that is to be reported/logged.
 * @param {string} qualMsg an optional qualifying message that
 * provides additional context to the supplied err.  This message
 * should be worded in such a way that makes sense when appended to:
 * 'An unexpected error occurred' ... for example consider: 'when
 * retrieving students'.
 *
 * @return {Action} a redux action object that MUST BE dispatched to
 * report the error to the user.
 */
export default function handleUnexpectedError(err, qualMsg='') {

  // log the details of the error (with traceback) for tech review
  const userMsg = `An unexpected error occurred ${qualMsg}.`;
  log.error(()=> userMsg, err);

  // setup the DETAIL callback for our userMsg userAction
  const detailAction = (event) => {

    // format the detailed message to display in our detailAction
    // ... NOTE: err.message will contain any applicable server logId (to report to tech support)
    let detailedMsg =  `${userMsg}
  
    ${err.clientMsg}
    ${err.message}

If this problem persists, please contact your tech support.`;

    // show user the details
    alert(`${detailedMsg}`);
  };

  // create/return the redux action to provide user communication
  return AC.userMsg.display(userMsg, 
                            {
                              txt:      'details',
                              callback: detailAction,
                            });
}<commit_msg>recognize Stale Data exception, guiding the user more delicately<commit_after>'use strict';

import React      from 'react';
import Log        from '../../shared/util/Log';
import {AC}       from '../actions';
import Alert      from '../comp/Alert';

const log = new Log('unexpectedError');

/**
 * The handleUnexpectedError() function provides a common utility to
 * consistently report/log client-side errors in a standard way.
 * 
 *  - The supplied err is appropriately logged.
 *
 *  - A redux action is created/returned that will provide appropriate
 *    communication to the user in a standardized way (with an
 *    optional details link to glean additional 'technical'
 *    information).
 *
 * NOTE: The returned action MUST BE dispatched (through the redux store)
 *       in order for the user to visualize the error condition.
 *
 * @param {Error} err the error that is to be reported/logged.
 * @param {string} qualMsg an optional qualifying message that
 * provides additional context to the supplied err.  This message
 * should be worded in such a way that makes sense when appended to:
 * 'An unexpected error occurred' ... for example consider: 'when
 * retrieving students'.
 *
 * @return {Action} a redux action object that MUST BE dispatched to
 * report the error to the user.
 */
export default function handleUnexpectedError(err, qualMsg='') {

  // stale data is NOT unexpected ... guide the user more delicately
  if (err.message.includes('Stale data detected')) {

    // log the details of the error (with traceback) for tech review
    const userMsg = `Your data is "out of date" ... ${qualMsg}.`;
    log.error(()=> userMsg, err);
    
    // create/return action providing user communication
    return AC.userMsg.display('Your data is "out of date" ... refresh and try again.',
                              {
                                txt:      'details',
                                callback: () => { // display more detail to user
                                  Alert.display({
                                    title: 'Stale Data',
                                    msg:   <div>
                                             {userMsg}
                                             <br/><br/>
                                             Someone has modified it since you last retrieved it.
                                             <br/><br/>
                                             Please refresh the data and try your operation again.
                                           </div>
                                  });
                                }
                              });
  }

  // an unexpected error
  else {

    // log the details of the error (with traceback) for tech review
    const userMsg = `An unexpected error occurred ${qualMsg}.`;
    log.error(()=> userMsg, err);
    
    // create/return action providing user communication
    return AC.userMsg.display(userMsg, 
                              {
                                txt:      'details',
                                callback: () => { // display more detail to user
                                  Alert.display({
                                    title: 'Unexpected Error',
                                           // NOTE: err.message (below) containd applicable server logId (if any) to report to tech support
                                    msg:   <div>
                                             {userMsg}
                                             <ul>
                                               {err.clientMsg}<br/>
                                               {err.message}
                                             </ul>
                                             If this problem persists, please contact your tech support.
                                           </div>
                                  });
                                }
                              });
  }
}
{'fraction_non_alphanumeric': 0.08286038592508513, 'fraction_numerical': 0.0, 'mean_word_length': 1.530980713992614, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6706816', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1328, 'n_words': 583}