dce-reactkit 3.2.0-beta.3 → 3.2.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -2573,6 +2573,18 @@ var LogSource;
2573
2573
  })(LogSource || (LogSource = {}));
2574
2574
  var LogSource$1 = LogSource;
2575
2575
 
2576
+ /**
2577
+ * Built-in categories for logs
2578
+ * @author Gabe Abrams
2579
+ */
2580
+ var LogBuiltInCategory;
2581
+ (function (LogBuiltInCategory) {
2582
+ LogBuiltInCategory["Uncategorized"] = "n/a";
2583
+ LogBuiltInCategory["ServerRenderedErrorPage"] = "_server-rendered-error-page";
2584
+ LogBuiltInCategory["ServerEndpointError"] = "_server-endpoint-error";
2585
+ })(LogBuiltInCategory || (LogBuiltInCategory = {}));
2586
+ var LogBuiltInCategory$1 = LogBuiltInCategory;
2587
+
2576
2588
  /**
2577
2589
  * Generate an express API route handler
2578
2590
  * @author Gabe Abrams
@@ -2897,86 +2909,136 @@ const genRouteHandler = (opts) => {
2897
2909
  * @author Gabe Abrams
2898
2910
  */
2899
2911
  const logServerEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
2912
+ var _c, _d, _e, _f, _g, _h, _j, _k;
2900
2913
  // NOTE: internally, we slip through an opts.overrideAsClientEvent boolean
2901
2914
  // that indicates that this is actually a client event, but we don't
2902
2915
  // include that in the LogFunction type because this is internal and
2903
2916
  // hidden from users
2904
- var _c, _d, _e, _f, _g, _h, _j, _k;
2905
- // Parse user agent
2906
- const { browser, device, } = parseUserAgent(req.headers['user-agent']);
2907
- // Get time info in ET
2908
- const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
2909
- // Main log info
2910
- const mainLogInfo = {
2911
- id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
2912
- userFirstName: launchInfo.userFirstName,
2913
- userLastName: launchInfo.userLastName,
2914
- userEmail: launchInfo.userEmail,
2915
- userId: launchInfo.userId,
2916
- isLearner: !!launchInfo.isLearner,
2917
- isAdmin: !!launchInfo.isAdmin,
2918
- isTTM: !!launchInfo.isTTM,
2919
- courseId: launchInfo.courseId,
2920
- courseName: launchInfo.courseName,
2921
- browser,
2922
- device,
2923
- year,
2924
- month,
2925
- day,
2926
- hour,
2927
- minute,
2928
- timestamp,
2929
- category: (typeof opts.category === 'string'
2930
- ? opts.category
2931
- : opts.category.name),
2932
- subcategory: (typeof opts.category === 'string'
2933
- ? opts.subcategory
2934
- : ((_c = opts.subcategory) !== null && _c !== void 0 ? _c : { name: 'none' }).name),
2935
- tags: (_d = opts.tags) !== null && _d !== void 0 ? _d : [],
2936
- metadata: (_e = opts.metadata) !== null && _e !== void 0 ? _e : {},
2937
- };
2938
- // Type-specific info
2939
- const typeSpecificInfo = (('error' in opts && opts.error)
2940
- ? {
2941
- type: LogType$1.Error,
2942
- errorMessage: (_f = opts.error.message) !== null && _f !== void 0 ? _f : 'Unknown message',
2943
- errorCode: (_g = opts.error.code) !== null && _g !== void 0 ? _g : ReactKitErrorCode$1.NoCode,
2944
- errorStack: (_h = opts.error.stack) !== null && _h !== void 0 ? _h : 'No stack',
2917
+ try {
2918
+ // Parse user agent
2919
+ const { browser, device, } = parseUserAgent(req.headers['user-agent']);
2920
+ // Get time info in ET
2921
+ const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
2922
+ // Main log info
2923
+ const mainLogInfo = {
2924
+ id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
2925
+ userFirstName: launchInfo.userFirstName,
2926
+ userLastName: launchInfo.userLastName,
2927
+ userEmail: launchInfo.userEmail,
2928
+ userId: launchInfo.userId,
2929
+ isLearner: !!launchInfo.isLearner,
2930
+ isAdmin: !!launchInfo.isAdmin,
2931
+ isTTM: !!launchInfo.isTTM,
2932
+ courseId: launchInfo.courseId,
2933
+ courseName: launchInfo.courseName,
2934
+ browser,
2935
+ device,
2936
+ year,
2937
+ month,
2938
+ day,
2939
+ hour,
2940
+ minute,
2941
+ timestamp,
2942
+ category: (typeof opts.category === 'string'
2943
+ ? opts.category
2944
+ : opts.category.name),
2945
+ subcategory: (typeof opts.category === 'string'
2946
+ ? opts.subcategory
2947
+ : ((_c = opts.subcategory) !== null && _c !== void 0 ? _c : {}).name),
2948
+ tags: (_d = opts.tags) !== null && _d !== void 0 ? _d : [],
2949
+ metadata: (_e = opts.metadata) !== null && _e !== void 0 ? _e : {},
2950
+ };
2951
+ // Type-specific info
2952
+ const typeSpecificInfo = (('error' in opts && opts.error)
2953
+ ? {
2954
+ type: LogType$1.Error,
2955
+ errorMessage: (_f = opts.error.message) !== null && _f !== void 0 ? _f : 'Unknown message',
2956
+ errorCode: (_g = opts.error.code) !== null && _g !== void 0 ? _g : ReactKitErrorCode$1.NoCode,
2957
+ errorStack: (_h = opts.error.stack) !== null && _h !== void 0 ? _h : 'No stack',
2958
+ }
2959
+ : {
2960
+ type: LogType$1.Action,
2961
+ target: (_j = opts.target) !== null && _j !== void 0 ? _j : 'Unknown',
2962
+ action: (_k = opts.action) !== null && _k !== void 0 ? _k : 'Unknown'
2963
+ });
2964
+ // Source-specific info
2965
+ const sourceSpecificInfo = (opts.overrideAsClientEvent
2966
+ ? {
2967
+ source: LogSource$1.Client,
2968
+ }
2969
+ : {
2970
+ source: LogSource$1.Server,
2971
+ routePath: req.path,
2972
+ routeTemplate: req.route.path,
2973
+ });
2974
+ // Build log event
2975
+ const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
2976
+ // Either print to console or save to db
2977
+ const logCollection = internalGetLogCollection();
2978
+ if (logCollection) {
2979
+ // Store to the log collection
2980
+ yield logCollection.insert(log);
2945
2981
  }
2946
- : {
2947
- type: LogType$1.Action,
2948
- target: (_j = opts.target) !== null && _j !== void 0 ? _j : 'Unknown',
2949
- action: (_k = opts.action) !== null && _k !== void 0 ? _k : 'Unknown'
2950
- });
2951
- // Source-specific info
2952
- const sourceSpecificInfo = (opts.overrideAsClientEvent
2953
- ? {
2954
- source: LogSource$1.Client,
2982
+ else {
2983
+ // Print to console
2984
+ if (log.type === LogType$1.Error) {
2985
+ console.error('dce-reactkit error log:', log);
2986
+ }
2987
+ else {
2988
+ console.log('dce-reactkit action log:', log);
2989
+ }
2955
2990
  }
2956
- : {
2991
+ // Return log entry
2992
+ return log;
2993
+ }
2994
+ catch (err) {
2995
+ // Print because we cannot store the error
2996
+ console.error('Could not log the following:', opts);
2997
+ // Create a dummy log to return
2998
+ const dummyMainInfo = {
2999
+ id: '-1',
3000
+ userFirstName: 'Unknown',
3001
+ userLastName: 'Unknown',
3002
+ userEmail: 'unknown@harvard.edu',
3003
+ userId: 1,
3004
+ isLearner: false,
3005
+ isAdmin: false,
3006
+ isTTM: false,
3007
+ courseId: 1,
3008
+ courseName: 'Unknown',
3009
+ browser: {
3010
+ name: 'Unknown',
3011
+ version: 'Unknown',
3012
+ },
3013
+ device: {
3014
+ isMobile: false,
3015
+ os: 'Unknown',
3016
+ },
3017
+ year: 1,
3018
+ month: 1,
3019
+ day: 1,
3020
+ hour: 1,
3021
+ minute: 1,
3022
+ timestamp: Date.now(),
3023
+ tags: [],
3024
+ metadata: {},
3025
+ category: LogBuiltInCategory$1.Uncategorized,
3026
+ subcategory: LogBuiltInCategory$1.Uncategorized,
3027
+ };
3028
+ const dummyTypeSpecificInfo = {
3029
+ type: LogType$1.Error,
3030
+ errorMessage: 'Unknown',
3031
+ errorCode: 'Unknown',
3032
+ errorStack: 'No Stack',
3033
+ };
3034
+ const dummySourceSpecificInfo = {
2957
3035
  source: LogSource$1.Server,
2958
3036
  routePath: req.path,
2959
3037
  routeTemplate: req.route.path,
2960
- });
2961
- // Build log event
2962
- const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
2963
- // Either print to console or save to db
2964
- const logCollection = internalGetLogCollection();
2965
- if (logCollection) {
2966
- // Store to the log collection
2967
- yield logCollection.insert(log);
2968
- }
2969
- else {
2970
- // Print to console
2971
- if (log.type === LogType$1.Error) {
2972
- console.error('dce-reactkit error log:', log);
2973
- }
2974
- else {
2975
- console.log('dce-reactkit action log:', log);
2976
- }
3038
+ };
3039
+ const log = Object.assign(Object.assign(Object.assign({}, dummyMainInfo), dummyTypeSpecificInfo), dummySourceSpecificInfo);
3040
+ return log;
2977
3041
  }
2978
- // Return log entry
2979
- return log;
2980
3042
  });
2981
3043
  /*------------------------------------------------------------------------*/
2982
3044
  /* Call handler */
@@ -3015,9 +3077,24 @@ const genRouteHandler = (opts) => {
3015
3077
  * @param [opts.status=500] http status code
3016
3078
  */
3017
3079
  const renderErrorPage = (opts = {}) => {
3018
- var _a;
3080
+ var _a, _b;
3019
3081
  const html = genErrorPage(opts);
3020
3082
  send(html, (_a = opts.status) !== null && _a !== void 0 ? _a : 500);
3083
+ // Log
3084
+ logServerEvent({
3085
+ category: LogBuiltInCategory$1.ServerRenderedErrorPage,
3086
+ error: {
3087
+ message: `${opts.title}: ${opts.description}`,
3088
+ code: opts.code,
3089
+ },
3090
+ metadata: {
3091
+ title: opts.title,
3092
+ description: opts.description,
3093
+ code: opts.code,
3094
+ pageTitle: opts.pageTitle,
3095
+ status: (_b = opts.status) !== null && _b !== void 0 ? _b : 500,
3096
+ },
3097
+ });
3021
3098
  };
3022
3099
  // Call the handler
3023
3100
  try {
@@ -3041,7 +3118,13 @@ const genRouteHandler = (opts) => {
3041
3118
  catch (err) {
3042
3119
  // Send error to client (only if next wasn't called)
3043
3120
  if (!responseSent) {
3044
- return handleError(res, err);
3121
+ handleError(res, err);
3122
+ // Log server-side error
3123
+ logServerEvent({
3124
+ category: LogBuiltInCategory$1.ServerEndpointError,
3125
+ error: err,
3126
+ });
3127
+ return;
3045
3128
  }
3046
3129
  // Log error that was not responded with
3047
3130
  console.log('Error occurred but could not be sent to client because a response was already sent:', err);
@@ -3345,5 +3428,5 @@ var LogAction;
3345
3428
  })(LogAction || (LogAction = {}));
3346
3429
  var LogAction$1 = LogAction;
3347
3430
 
3348
- export { AppWrapper, ButtonInputGroup, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, ErrorBox, ErrorWithCode, HOUR_IN_MS, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, ceilToNumDecimals, confirm, floorToNumDecimals, forceNumIntoBounds, genRouteHandler, getHumanReadableDate, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initLogCollection, initServer, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
3431
+ export { AppWrapper, ButtonInputGroup, CheckboxButton, CopiableBox, DAY_IN_MS, DayOfWeek$1 as DayOfWeek, Drawer, ErrorBox, ErrorWithCode, HOUR_IN_MS, ItemPicker, LoadingSpinner, LogAction$1 as LogAction, LogBuiltInCategory$1 as LogBuiltInCategory, LogSource$1 as LogSource, LogType$1 as LogType, MINUTE_IN_MS, Modal, ModalButtonType$1 as ModalButtonType, ModalSize$1 as ModalSize, ModalType$1 as ModalType, ParamType$1 as ParamType, PopFailureMark, PopPendingMark, PopSuccessMark, RadioButton, ReactKitErrorCode$1 as ReactKitErrorCode, SimpleDateChooser, TabBox, Variant$1 as Variant, abbreviate, alert$1 as alert, avg, ceilToNumDecimals, confirm, floorToNumDecimals, forceNumIntoBounds, genRouteHandler, getHumanReadableDate, getOrdinal, getPartOfDay, getTimeInfoInET, handleError, handleSuccess, initLogCollection, initServer, logClientEvent, onlyKeepLetters, padDecimalZeros, padZerosLeft, parallelLimit, roundToNumDecimals, showFatalError, startMinWait, stringsToHumanReadableList, stubServerEndpoint, sum, visitServerEndpoint, waitMs };
3349
3432
  //# sourceMappingURL=index.js.map