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.
@@ -0,0 +1,5 @@
1
+ {
2
+ "cSpell.words": [
3
+ "Uncategorized"
4
+ ]
5
+ }
package/dist/cjs/index.js CHANGED
@@ -2581,6 +2581,18 @@ var LogSource;
2581
2581
  })(LogSource || (LogSource = {}));
2582
2582
  var LogSource$1 = LogSource;
2583
2583
 
2584
+ /**
2585
+ * Built-in categories for logs
2586
+ * @author Gabe Abrams
2587
+ */
2588
+ var LogBuiltInCategory;
2589
+ (function (LogBuiltInCategory) {
2590
+ LogBuiltInCategory["Uncategorized"] = "n/a";
2591
+ LogBuiltInCategory["ServerRenderedErrorPage"] = "_server-rendered-error-page";
2592
+ LogBuiltInCategory["ServerEndpointError"] = "_server-endpoint-error";
2593
+ })(LogBuiltInCategory || (LogBuiltInCategory = {}));
2594
+ var LogBuiltInCategory$1 = LogBuiltInCategory;
2595
+
2584
2596
  /**
2585
2597
  * Generate an express API route handler
2586
2598
  * @author Gabe Abrams
@@ -2905,86 +2917,136 @@ const genRouteHandler = (opts) => {
2905
2917
  * @author Gabe Abrams
2906
2918
  */
2907
2919
  const logServerEvent = (opts) => __awaiter(void 0, void 0, void 0, function* () {
2920
+ var _c, _d, _e, _f, _g, _h, _j, _k;
2908
2921
  // NOTE: internally, we slip through an opts.overrideAsClientEvent boolean
2909
2922
  // that indicates that this is actually a client event, but we don't
2910
2923
  // include that in the LogFunction type because this is internal and
2911
2924
  // hidden from users
2912
- var _c, _d, _e, _f, _g, _h, _j, _k;
2913
- // Parse user agent
2914
- const { browser, device, } = parseUserAgent(req.headers['user-agent']);
2915
- // Get time info in ET
2916
- const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
2917
- // Main log info
2918
- const mainLogInfo = {
2919
- id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
2920
- userFirstName: launchInfo.userFirstName,
2921
- userLastName: launchInfo.userLastName,
2922
- userEmail: launchInfo.userEmail,
2923
- userId: launchInfo.userId,
2924
- isLearner: !!launchInfo.isLearner,
2925
- isAdmin: !!launchInfo.isAdmin,
2926
- isTTM: !!launchInfo.isTTM,
2927
- courseId: launchInfo.courseId,
2928
- courseName: launchInfo.courseName,
2929
- browser,
2930
- device,
2931
- year,
2932
- month,
2933
- day,
2934
- hour,
2935
- minute,
2936
- timestamp,
2937
- category: (typeof opts.category === 'string'
2938
- ? opts.category
2939
- : opts.category.name),
2940
- subcategory: (typeof opts.category === 'string'
2941
- ? opts.subcategory
2942
- : ((_c = opts.subcategory) !== null && _c !== void 0 ? _c : { name: 'none' }).name),
2943
- tags: (_d = opts.tags) !== null && _d !== void 0 ? _d : [],
2944
- metadata: (_e = opts.metadata) !== null && _e !== void 0 ? _e : {},
2945
- };
2946
- // Type-specific info
2947
- const typeSpecificInfo = (('error' in opts && opts.error)
2948
- ? {
2949
- type: LogType$1.Error,
2950
- errorMessage: (_f = opts.error.message) !== null && _f !== void 0 ? _f : 'Unknown message',
2951
- errorCode: (_g = opts.error.code) !== null && _g !== void 0 ? _g : ReactKitErrorCode$1.NoCode,
2952
- errorStack: (_h = opts.error.stack) !== null && _h !== void 0 ? _h : 'No stack',
2925
+ try {
2926
+ // Parse user agent
2927
+ const { browser, device, } = parseUserAgent(req.headers['user-agent']);
2928
+ // Get time info in ET
2929
+ const { timestamp, year, month, day, hour, minute, } = getTimeInfoInET();
2930
+ // Main log info
2931
+ const mainLogInfo = {
2932
+ id: `${launchInfo.userId}-${Date.now()}-${Math.floor(Math.random() * 100000)}-${Math.floor(Math.random() * 100000)}`,
2933
+ userFirstName: launchInfo.userFirstName,
2934
+ userLastName: launchInfo.userLastName,
2935
+ userEmail: launchInfo.userEmail,
2936
+ userId: launchInfo.userId,
2937
+ isLearner: !!launchInfo.isLearner,
2938
+ isAdmin: !!launchInfo.isAdmin,
2939
+ isTTM: !!launchInfo.isTTM,
2940
+ courseId: launchInfo.courseId,
2941
+ courseName: launchInfo.courseName,
2942
+ browser,
2943
+ device,
2944
+ year,
2945
+ month,
2946
+ day,
2947
+ hour,
2948
+ minute,
2949
+ timestamp,
2950
+ category: (typeof opts.category === 'string'
2951
+ ? opts.category
2952
+ : opts.category.name),
2953
+ subcategory: (typeof opts.category === 'string'
2954
+ ? opts.subcategory
2955
+ : ((_c = opts.subcategory) !== null && _c !== void 0 ? _c : {}).name),
2956
+ tags: (_d = opts.tags) !== null && _d !== void 0 ? _d : [],
2957
+ metadata: (_e = opts.metadata) !== null && _e !== void 0 ? _e : {},
2958
+ };
2959
+ // Type-specific info
2960
+ const typeSpecificInfo = (('error' in opts && opts.error)
2961
+ ? {
2962
+ type: LogType$1.Error,
2963
+ errorMessage: (_f = opts.error.message) !== null && _f !== void 0 ? _f : 'Unknown message',
2964
+ errorCode: (_g = opts.error.code) !== null && _g !== void 0 ? _g : ReactKitErrorCode$1.NoCode,
2965
+ errorStack: (_h = opts.error.stack) !== null && _h !== void 0 ? _h : 'No stack',
2966
+ }
2967
+ : {
2968
+ type: LogType$1.Action,
2969
+ target: (_j = opts.target) !== null && _j !== void 0 ? _j : 'Unknown',
2970
+ action: (_k = opts.action) !== null && _k !== void 0 ? _k : 'Unknown'
2971
+ });
2972
+ // Source-specific info
2973
+ const sourceSpecificInfo = (opts.overrideAsClientEvent
2974
+ ? {
2975
+ source: LogSource$1.Client,
2976
+ }
2977
+ : {
2978
+ source: LogSource$1.Server,
2979
+ routePath: req.path,
2980
+ routeTemplate: req.route.path,
2981
+ });
2982
+ // Build log event
2983
+ const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
2984
+ // Either print to console or save to db
2985
+ const logCollection = internalGetLogCollection();
2986
+ if (logCollection) {
2987
+ // Store to the log collection
2988
+ yield logCollection.insert(log);
2953
2989
  }
2954
- : {
2955
- type: LogType$1.Action,
2956
- target: (_j = opts.target) !== null && _j !== void 0 ? _j : 'Unknown',
2957
- action: (_k = opts.action) !== null && _k !== void 0 ? _k : 'Unknown'
2958
- });
2959
- // Source-specific info
2960
- const sourceSpecificInfo = (opts.overrideAsClientEvent
2961
- ? {
2962
- source: LogSource$1.Client,
2990
+ else {
2991
+ // Print to console
2992
+ if (log.type === LogType$1.Error) {
2993
+ console.error('dce-reactkit error log:', log);
2994
+ }
2995
+ else {
2996
+ console.log('dce-reactkit action log:', log);
2997
+ }
2963
2998
  }
2964
- : {
2999
+ // Return log entry
3000
+ return log;
3001
+ }
3002
+ catch (err) {
3003
+ // Print because we cannot store the error
3004
+ console.error('Could not log the following:', opts);
3005
+ // Create a dummy log to return
3006
+ const dummyMainInfo = {
3007
+ id: '-1',
3008
+ userFirstName: 'Unknown',
3009
+ userLastName: 'Unknown',
3010
+ userEmail: 'unknown@harvard.edu',
3011
+ userId: 1,
3012
+ isLearner: false,
3013
+ isAdmin: false,
3014
+ isTTM: false,
3015
+ courseId: 1,
3016
+ courseName: 'Unknown',
3017
+ browser: {
3018
+ name: 'Unknown',
3019
+ version: 'Unknown',
3020
+ },
3021
+ device: {
3022
+ isMobile: false,
3023
+ os: 'Unknown',
3024
+ },
3025
+ year: 1,
3026
+ month: 1,
3027
+ day: 1,
3028
+ hour: 1,
3029
+ minute: 1,
3030
+ timestamp: Date.now(),
3031
+ tags: [],
3032
+ metadata: {},
3033
+ category: LogBuiltInCategory$1.Uncategorized,
3034
+ subcategory: LogBuiltInCategory$1.Uncategorized,
3035
+ };
3036
+ const dummyTypeSpecificInfo = {
3037
+ type: LogType$1.Error,
3038
+ errorMessage: 'Unknown',
3039
+ errorCode: 'Unknown',
3040
+ errorStack: 'No Stack',
3041
+ };
3042
+ const dummySourceSpecificInfo = {
2965
3043
  source: LogSource$1.Server,
2966
3044
  routePath: req.path,
2967
3045
  routeTemplate: req.route.path,
2968
- });
2969
- // Build log event
2970
- const log = Object.assign(Object.assign(Object.assign({}, mainLogInfo), typeSpecificInfo), sourceSpecificInfo);
2971
- // Either print to console or save to db
2972
- const logCollection = internalGetLogCollection();
2973
- if (logCollection) {
2974
- // Store to the log collection
2975
- yield logCollection.insert(log);
2976
- }
2977
- else {
2978
- // Print to console
2979
- if (log.type === LogType$1.Error) {
2980
- console.error('dce-reactkit error log:', log);
2981
- }
2982
- else {
2983
- console.log('dce-reactkit action log:', log);
2984
- }
3046
+ };
3047
+ const log = Object.assign(Object.assign(Object.assign({}, dummyMainInfo), dummyTypeSpecificInfo), dummySourceSpecificInfo);
3048
+ return log;
2985
3049
  }
2986
- // Return log entry
2987
- return log;
2988
3050
  });
2989
3051
  /*------------------------------------------------------------------------*/
2990
3052
  /* Call handler */
@@ -3023,9 +3085,24 @@ const genRouteHandler = (opts) => {
3023
3085
  * @param [opts.status=500] http status code
3024
3086
  */
3025
3087
  const renderErrorPage = (opts = {}) => {
3026
- var _a;
3088
+ var _a, _b;
3027
3089
  const html = genErrorPage(opts);
3028
3090
  send(html, (_a = opts.status) !== null && _a !== void 0 ? _a : 500);
3091
+ // Log
3092
+ logServerEvent({
3093
+ category: LogBuiltInCategory$1.ServerRenderedErrorPage,
3094
+ error: {
3095
+ message: `${opts.title}: ${opts.description}`,
3096
+ code: opts.code,
3097
+ },
3098
+ metadata: {
3099
+ title: opts.title,
3100
+ description: opts.description,
3101
+ code: opts.code,
3102
+ pageTitle: opts.pageTitle,
3103
+ status: (_b = opts.status) !== null && _b !== void 0 ? _b : 500,
3104
+ },
3105
+ });
3029
3106
  };
3030
3107
  // Call the handler
3031
3108
  try {
@@ -3049,7 +3126,13 @@ const genRouteHandler = (opts) => {
3049
3126
  catch (err) {
3050
3127
  // Send error to client (only if next wasn't called)
3051
3128
  if (!responseSent) {
3052
- return handleError(res, err);
3129
+ handleError(res, err);
3130
+ // Log server-side error
3131
+ logServerEvent({
3132
+ category: LogBuiltInCategory$1.ServerEndpointError,
3133
+ error: err,
3134
+ });
3135
+ return;
3053
3136
  }
3054
3137
  // Log error that was not responded with
3055
3138
  console.log('Error occurred but could not be sent to client because a response was already sent:', err);
@@ -3366,6 +3449,7 @@ exports.HOUR_IN_MS = HOUR_IN_MS;
3366
3449
  exports.ItemPicker = ItemPicker;
3367
3450
  exports.LoadingSpinner = LoadingSpinner;
3368
3451
  exports.LogAction = LogAction$1;
3452
+ exports.LogBuiltInCategory = LogBuiltInCategory$1;
3369
3453
  exports.LogSource = LogSource$1;
3370
3454
  exports.LogType = LogType$1;
3371
3455
  exports.MINUTE_IN_MS = MINUTE_IN_MS;