dce-reactkit 2.0.3 → 2.0.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/lib/components/AppWrapper.d.ts +19 -0
- package/lib/components/AppWrapper.js +127 -0
- package/lib/components/AppWrapper.js.map +1 -0
- package/lib/components/ErrorBox.d.ts +12 -0
- package/lib/components/ErrorBox.js +65 -0
- package/lib/components/ErrorBox.js.map +1 -0
- package/lib/helpers/handleError.d.ts +14 -0
- package/lib/helpers/handleError.js +54 -0
- package/lib/helpers/handleError.js.map +1 -0
- package/lib/helpers/handleSuccess.d.ts +8 -0
- package/lib/helpers/handleSuccess.js +20 -0
- package/lib/helpers/handleSuccess.js.map +1 -0
- package/lib/helpers/parseRequest.d.ts +21 -0
- package/lib/helpers/parseRequest.js +240 -0
- package/lib/helpers/parseRequest.js.map +1 -0
- package/lib/helpers/showFatalError.d.ts +2 -0
- package/lib/helpers/showFatalError.js +5 -0
- package/lib/helpers/showFatalError.js.map +1 -0
- package/lib/helpers/visitServerEndpoint.d.ts +7 -1
- package/lib/helpers/visitServerEndpoint.js +57 -6
- package/lib/helpers/visitServerEndpoint.js.map +1 -1
- package/lib/types/ParamType.d.ts +17 -0
- package/lib/types/ParamType.js +21 -0
- package/lib/types/ParamType.js.map +1 -0
- package/lib/types/ReactKitErrorCode.d.ts +5 -1
- package/lib/types/ReactKitErrorCode.js +4 -0
- package/lib/types/ReactKitErrorCode.js.map +1 -1
- package/package.json +2 -1
- package/src/components/AppWrapper.tsx +55 -5
- package/src/components/ErrorBox.tsx +4 -1
- package/src/helpers/handleError.ts +65 -0
- package/src/helpers/handleSuccess.ts +18 -0
- package/src/helpers/parseRequest.ts +299 -0
- package/src/helpers/visitServerEndpoint.tsx +52 -1
- package/src/types/ParamType.ts +18 -0
- package/src/types/ReactKitErrorCode.tsx +4 -0
- package/tsconfig.json +1 -1
|
@@ -39,13 +39,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.setSessionExpiryHandler = void 0;
|
|
42
43
|
// Initialize caccl
|
|
43
44
|
var client_1 = require("caccl/client");
|
|
44
45
|
// Import custom error
|
|
45
46
|
var ErrorWithCode_1 = __importDefault(require("../errors/ErrorWithCode"));
|
|
46
47
|
var ReactKitErrorCode_1 = __importDefault(require("../types/ReactKitErrorCode"));
|
|
48
|
+
/*------------------------------------------------------------------------*/
|
|
49
|
+
/* Listener */
|
|
50
|
+
/*------------------------------------------------------------------------*/
|
|
51
|
+
// Handler for session expiry
|
|
52
|
+
var sessionExpiryHandler;
|
|
53
|
+
// Keep track of whether or not session expiry has already been handled
|
|
54
|
+
var sessionAlreadyExpired = false;
|
|
47
55
|
/**
|
|
48
|
-
*
|
|
56
|
+
* Set the session expiry handler
|
|
57
|
+
* @author Gabe Abrams
|
|
58
|
+
* @param handler new handler to use when session expires
|
|
59
|
+
*/
|
|
60
|
+
var setSessionExpiryHandler = function (handler) {
|
|
61
|
+
sessionExpiryHandler = handler;
|
|
62
|
+
};
|
|
63
|
+
exports.setSessionExpiryHandler = setSessionExpiryHandler;
|
|
64
|
+
/*------------------------------------------------------------------------*/
|
|
65
|
+
/* Main */
|
|
66
|
+
/*------------------------------------------------------------------------*/
|
|
67
|
+
/**
|
|
68
|
+
* Visit an endpoint on the server [for client only]
|
|
49
69
|
* @author Gabe Abrams
|
|
50
70
|
* @param opts object containing all arguments
|
|
51
71
|
* @param opts.path - the path of the server endpoint
|
|
@@ -68,12 +88,43 @@ var visitServerEndpoint = function (opts) { return __awaiter(void 0, void 0, voi
|
|
|
68
88
|
if (!response || !response.body) {
|
|
69
89
|
throw new ErrorWithCode_1.default('We didn\'t get a response from the server. Please check your internet connection.', ReactKitErrorCode_1.default.NoResponse);
|
|
70
90
|
}
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
91
|
+
if (!!response.body.success) return [3 /*break*/, 6];
|
|
92
|
+
if (!(response.body.code === ReactKitErrorCode_1.default.SessionExpired)) return [3 /*break*/, 5];
|
|
93
|
+
if (!sessionAlreadyExpired) return [3 /*break*/, 3];
|
|
94
|
+
// Never return (browser is already reloading)
|
|
95
|
+
return [4 /*yield*/, new Promise(function () {
|
|
96
|
+
// Promise that never returns
|
|
97
|
+
})];
|
|
98
|
+
case 2:
|
|
99
|
+
// Never return (browser is already reloading)
|
|
100
|
+
_a.sent();
|
|
101
|
+
_a.label = 3;
|
|
102
|
+
case 3:
|
|
103
|
+
sessionAlreadyExpired = true;
|
|
104
|
+
// Show session expiration message
|
|
105
|
+
if (sessionExpiryHandler) {
|
|
106
|
+
// Use handler
|
|
107
|
+
sessionExpiryHandler();
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
// Fallback to alert
|
|
111
|
+
// eslint-disable-next-line no-alert
|
|
112
|
+
alert('Your session has expired. Please start over.');
|
|
76
113
|
}
|
|
114
|
+
// Never return (don't continue execution)
|
|
115
|
+
return [4 /*yield*/, new Promise(function () {
|
|
116
|
+
// Promise that never returns
|
|
117
|
+
})];
|
|
118
|
+
case 4:
|
|
119
|
+
// Never return (don't continue execution)
|
|
120
|
+
_a.sent();
|
|
121
|
+
_a.label = 5;
|
|
122
|
+
case 5:
|
|
123
|
+
// Other errors
|
|
124
|
+
throw new ErrorWithCode_1.default((response.body.message
|
|
125
|
+
|| 'An unknown error occurred. Please contact an admin.'), (response.body.code
|
|
126
|
+
|| ReactKitErrorCode_1.default.NoCode));
|
|
127
|
+
case 6:
|
|
77
128
|
body = response.body.body;
|
|
78
129
|
// Return
|
|
79
130
|
return [2 /*return*/, body];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"visitServerEndpoint.js","sourceRoot":"","sources":["../../src/helpers/visitServerEndpoint.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"visitServerEndpoint.js","sourceRoot":"","sources":["../../src/helpers/visitServerEndpoint.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mBAAmB;AACnB,uCAA2C;AAE3C,sBAAsB;AACtB,0EAAoD;AACpD,iFAA2D;AAE3D,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAE5E,6BAA6B;AAC7B,IAAI,oBAAgC,CAAC;AAErC,uEAAuE;AACvE,IAAI,qBAAqB,GAAG,KAAK,CAAC;AAElC;;;;GAIG;AACI,IAAM,uBAAuB,GAAG,UAAC,OAAmB;IACzD,oBAAoB,GAAG,OAAO,CAAC;AACjC,CAAC,CAAC;AAFW,QAAA,uBAAuB,2BAElC;AAEF,4EAA4E;AAC5E,4EAA4E;AAC5E,4EAA4E;AAE5E;;;;;;;;GAQG;AACH,IAAM,mBAAmB,GAAG,UAC1B,IAIC;;;;oBAGgB,qBAAM,IAAA,oBAAW,EAAC;oBACjC,IAAI,EAAE,IAAI,CAAC,IAAI;oBACf,MAAM,EAAE,IAAI,CAAC,MAAM;oBACnB,MAAM,EAAE,IAAI,CAAC,MAAM;iBACpB,CAAC,EAAA;;gBAJI,QAAQ,GAAG,SAIf;gBAEF,oBAAoB;gBACpB,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;oBAC/B,MAAM,IAAI,uBAAa,CACrB,mFAAmF,EACnF,2BAAiB,CAAC,UAAU,CAC7B,CAAC;iBACH;qBACG,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAtB,wBAAsB;qBAEpB,CAAA,QAAQ,CAAC,IAAI,CAAC,IAAI,KAAK,2BAAiB,CAAC,cAAc,CAAA,EAAvD,wBAAuD;qBAErD,qBAAqB,EAArB,wBAAqB;gBACvB,8CAA8C;gBAC9C,qBAAM,IAAI,OAAO,CAA2B;wBAC1C,6BAA6B;oBAC/B,CAAC,CAAC,EAAA;;gBAHF,8CAA8C;gBAC9C,SAEE,CAAC;;;gBAEL,qBAAqB,GAAG,IAAI,CAAC;gBAE7B,kCAAkC;gBAClC,IAAI,oBAAoB,EAAE;oBACxB,cAAc;oBACd,oBAAoB,EAAE,CAAC;iBACxB;qBAAM;oBACL,oBAAoB;oBAEpB,oCAAoC;oBACpC,KAAK,CAAC,8CAA8C,CAAC,CAAC;iBACvD;gBAED,0CAA0C;gBAC1C,qBAAM,IAAI,OAAO,CAA2B;wBAC1C,6BAA6B;oBAC/B,CAAC,CAAC,EAAA;;gBAHF,0CAA0C;gBAC1C,SAEE,CAAC;;;YAGL,eAAe;YACf,MAAM,IAAI,uBAAa,CACrB,CACE,QAAQ,CAAC,IAAI,CAAC,OAAO;mBAClB,qDAAqD,CACzD,EACD,CACE,QAAQ,CAAC,IAAI,CAAC,IAAI;mBACf,2BAAiB,CAAC,MAAM,CAC5B,CACF,CAAC;;gBAII,IAAI,GAAK,QAAQ,CAAC,IAAI,KAAlB,CAAmB;gBAE/B,SAAS;gBACT,sBAAO,IAAI,EAAC;;;KACb,CAAC;AAEF,kBAAe,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API param types
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
*/
|
|
5
|
+
declare enum ParamType {
|
|
6
|
+
Boolean = "boolean",
|
|
7
|
+
BooleanOptional = "boolean-optional",
|
|
8
|
+
Float = "float",
|
|
9
|
+
FloatOptional = "float-optional",
|
|
10
|
+
Int = "int",
|
|
11
|
+
IntOptional = "int-optional",
|
|
12
|
+
JSON = "json",
|
|
13
|
+
JSONOptional = "json-optional",
|
|
14
|
+
String = "string",
|
|
15
|
+
StringOptional = "string-optional"
|
|
16
|
+
}
|
|
17
|
+
export default ParamType;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* API param types
|
|
5
|
+
* @author Gabe Abrams
|
|
6
|
+
*/
|
|
7
|
+
var ParamType;
|
|
8
|
+
(function (ParamType) {
|
|
9
|
+
ParamType["Boolean"] = "boolean";
|
|
10
|
+
ParamType["BooleanOptional"] = "boolean-optional";
|
|
11
|
+
ParamType["Float"] = "float";
|
|
12
|
+
ParamType["FloatOptional"] = "float-optional";
|
|
13
|
+
ParamType["Int"] = "int";
|
|
14
|
+
ParamType["IntOptional"] = "int-optional";
|
|
15
|
+
ParamType["JSON"] = "json";
|
|
16
|
+
ParamType["JSONOptional"] = "json-optional";
|
|
17
|
+
ParamType["String"] = "string";
|
|
18
|
+
ParamType["StringOptional"] = "string-optional";
|
|
19
|
+
})(ParamType || (ParamType = {}));
|
|
20
|
+
exports.default = ParamType;
|
|
21
|
+
//# sourceMappingURL=ParamType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ParamType.js","sourceRoot":"","sources":["../../src/types/ParamType.ts"],"names":[],"mappings":";;AAAA;;;GAGG;AACH,IAAK,SAWJ;AAXD,WAAK,SAAS;IACZ,gCAAmB,CAAA;IACnB,iDAAoC,CAAA;IACpC,4BAAe,CAAA;IACf,6CAAgC,CAAA;IAChC,wBAAW,CAAA;IACX,yCAA4B,CAAA;IAC5B,0BAAa,CAAA;IACb,2CAA8B,CAAA;IAC9B,8BAAiB,CAAA;IACjB,+CAAkC,CAAA;AACpC,CAAC,EAXI,SAAS,KAAT,SAAS,QAWb;AAED,kBAAe,SAAS,CAAC"}
|
|
@@ -9,6 +9,10 @@ var ReactKitErrorCode;
|
|
|
9
9
|
(function (ReactKitErrorCode) {
|
|
10
10
|
ReactKitErrorCode["NoResponse"] = "DRK1";
|
|
11
11
|
ReactKitErrorCode["NoCode"] = "DRK2";
|
|
12
|
+
ReactKitErrorCode["SessionExpired"] = "DRK3";
|
|
13
|
+
ReactKitErrorCode["MissingParameter"] = "DRK4";
|
|
14
|
+
ReactKitErrorCode["InvalidParameter"] = "DRK5";
|
|
15
|
+
ReactKitErrorCode["WrongCourse"] = "DRK6";
|
|
12
16
|
})(ReactKitErrorCode || (ReactKitErrorCode = {}));
|
|
13
17
|
exports.default = ReactKitErrorCode;
|
|
14
18
|
//# sourceMappingURL=ReactKitErrorCode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ReactKitErrorCode.js","sourceRoot":"","sources":["../../src/types/ReactKitErrorCode.tsx"],"names":[],"mappings":";AAAA,4BAA4B;;AAE5B;;;GAGG;AACH,IAAK,
|
|
1
|
+
{"version":3,"file":"ReactKitErrorCode.js","sourceRoot":"","sources":["../../src/types/ReactKitErrorCode.tsx"],"names":[],"mappings":";AAAA,4BAA4B;;AAE5B;;;GAGG;AACH,IAAK,iBAOJ;AAPD,WAAK,iBAAiB;IACpB,wCAAmB,CAAA;IACnB,oCAAe,CAAA;IACf,4CAAuB,CAAA;IACvB,8CAAyB,CAAA;IACzB,8CAAyB,CAAA;IACzB,yCAAoB,CAAA;AACtB,CAAC,EAPI,iBAAiB,KAAjB,iBAAiB,QAOrB;AAED,kBAAe,iBAAiB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dce-reactkit",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"module": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"react-bootstrap": "^2.2.3"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
+
"@types/express": "^4.17.13",
|
|
25
26
|
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
26
27
|
"@typescript-eslint/parser": "^5.18.0",
|
|
27
28
|
"eslint": "^8.12.0",
|
|
@@ -5,11 +5,14 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
// Import React
|
|
8
|
-
import React, { useState } from 'react';
|
|
8
|
+
import React, { useState, useEffect } from 'react';
|
|
9
9
|
|
|
10
10
|
// Import shared components
|
|
11
11
|
import ErrorBox from './ErrorBox';
|
|
12
12
|
|
|
13
|
+
// Import helpers
|
|
14
|
+
import { setSessionExpiryHandler } from '../helpers/visitServerEndpoint';
|
|
15
|
+
|
|
13
16
|
// Import shared types
|
|
14
17
|
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
15
18
|
|
|
@@ -25,6 +28,8 @@ type Props = {
|
|
|
25
28
|
children: React.ReactNode,
|
|
26
29
|
// True if this app is a dark-themed app
|
|
27
30
|
dark?: boolean,
|
|
31
|
+
// Custom session expired message
|
|
32
|
+
sessionExpiredMessage?: string,
|
|
28
33
|
};
|
|
29
34
|
|
|
30
35
|
/*------------------------------------------------------------------------*/
|
|
@@ -77,6 +82,7 @@ const AppWrapper = (props: Props) => {
|
|
|
77
82
|
const {
|
|
78
83
|
children,
|
|
79
84
|
dark,
|
|
85
|
+
sessionExpiredMessage = 'Your session has expired. Please go back to Canvas and start over.',
|
|
80
86
|
} = props;
|
|
81
87
|
|
|
82
88
|
/* -------------- State ------------- */
|
|
@@ -87,18 +93,52 @@ const AppWrapper = (props: Props) => {
|
|
|
87
93
|
const [fatalErrorCode, setFatalErrorCodeInner] = useState(null);
|
|
88
94
|
setFatalErrorCode = setFatalErrorCodeInner;
|
|
89
95
|
|
|
96
|
+
// Session expired
|
|
97
|
+
const [sessionHasExpired, setSessionHasExpired] = useState(false);
|
|
98
|
+
|
|
99
|
+
/*------------------------------------------------------------------------*/
|
|
100
|
+
/* Lifecycle Functions */
|
|
101
|
+
/*------------------------------------------------------------------------*/
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Mount
|
|
105
|
+
* @author Gabe Abrams
|
|
106
|
+
*/
|
|
107
|
+
useEffect(
|
|
108
|
+
() => {
|
|
109
|
+
// Add session expired handler
|
|
110
|
+
setSessionExpiryHandler(() => {
|
|
111
|
+
// Session expired!
|
|
112
|
+
setSessionHasExpired(true);
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
[],
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
/*------------------------------------------------------------------------*/
|
|
119
|
+
/* Render */
|
|
120
|
+
/*------------------------------------------------------------------------*/
|
|
121
|
+
|
|
90
122
|
/*----------------------------------------*/
|
|
91
123
|
/* Main UI */
|
|
92
124
|
/*----------------------------------------*/
|
|
93
125
|
|
|
94
126
|
// Show error
|
|
95
|
-
if (fatalErrorMessage || fatalErrorCode) {
|
|
127
|
+
if (fatalErrorMessage || fatalErrorCode || sessionHasExpired) {
|
|
96
128
|
// Re-encapsulate in an error
|
|
97
|
-
const error =
|
|
98
|
-
|
|
99
|
-
|
|
129
|
+
const error = (
|
|
130
|
+
sessionHasExpired
|
|
131
|
+
? new ErrorWithCode(
|
|
132
|
+
fatalErrorMessage,
|
|
133
|
+
fatalErrorCode,
|
|
134
|
+
)
|
|
135
|
+
: new ErrorWithCode(
|
|
136
|
+
sessionExpiredMessage,
|
|
137
|
+
ReactKitErrorCode.SessionExpired,
|
|
138
|
+
)
|
|
100
139
|
);
|
|
101
140
|
|
|
141
|
+
// Build error screen
|
|
102
142
|
return (
|
|
103
143
|
<div
|
|
104
144
|
style={{
|
|
@@ -106,9 +146,19 @@ const AppWrapper = (props: Props) => {
|
|
|
106
146
|
width: '100vw',
|
|
107
147
|
minHeight: '100vh',
|
|
108
148
|
paddingTop: '10rem',
|
|
149
|
+
backgroundColor: (
|
|
150
|
+
dark
|
|
151
|
+
? '#222'
|
|
152
|
+
: '#fff'
|
|
153
|
+
),
|
|
109
154
|
}}
|
|
110
155
|
>
|
|
111
156
|
<ErrorBox
|
|
157
|
+
title={(
|
|
158
|
+
sessionHasExpired
|
|
159
|
+
? 'Session Expired'
|
|
160
|
+
: undefined
|
|
161
|
+
)}
|
|
112
162
|
error={error}
|
|
113
163
|
/>
|
|
114
164
|
</div>
|
|
@@ -20,6 +20,8 @@ import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
|
20
20
|
type Props = {
|
|
21
21
|
// Error to display
|
|
22
22
|
error: any,
|
|
23
|
+
// Customized title of error box
|
|
24
|
+
title?: string,
|
|
23
25
|
// Handler for close,
|
|
24
26
|
onClose?: () => void,
|
|
25
27
|
};
|
|
@@ -37,6 +39,7 @@ const ErrorBox: React.FC<Props> = (props) => {
|
|
|
37
39
|
|
|
38
40
|
const {
|
|
39
41
|
error,
|
|
42
|
+
title = 'An Error Occurred',
|
|
40
43
|
onClose,
|
|
41
44
|
} = props;
|
|
42
45
|
|
|
@@ -91,7 +94,7 @@ const ErrorBox: React.FC<Props> = (props) => {
|
|
|
91
94
|
icon={faExclamationTriangle}
|
|
92
95
|
className="me-2"
|
|
93
96
|
/>
|
|
94
|
-
|
|
97
|
+
{title}
|
|
95
98
|
</h4>
|
|
96
99
|
<div>
|
|
97
100
|
{errorText}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Import shared types
|
|
2
|
+
import ReactKitErrorCode from '../types/ReactKitErrorCode';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Handle an error and respond to the client
|
|
6
|
+
* @author Gabe Abrams
|
|
7
|
+
* @param res express response
|
|
8
|
+
* @param error error info
|
|
9
|
+
* @param opts.err the error to send to the client
|
|
10
|
+
* or the error message
|
|
11
|
+
* @param [opts.code] an error code (only used if err.code is not
|
|
12
|
+
* included)
|
|
13
|
+
* @param [opts.status=500] the https status code to use
|
|
14
|
+
* defined)
|
|
15
|
+
*/
|
|
16
|
+
const handleError = (
|
|
17
|
+
res: any,
|
|
18
|
+
error: (
|
|
19
|
+
| {
|
|
20
|
+
message: any,
|
|
21
|
+
code?: string,
|
|
22
|
+
status?: number,
|
|
23
|
+
}
|
|
24
|
+
| Error
|
|
25
|
+
| string
|
|
26
|
+
| any
|
|
27
|
+
),
|
|
28
|
+
): undefined => {
|
|
29
|
+
// Get the error message
|
|
30
|
+
let message;
|
|
31
|
+
if (error && (error as any).message) {
|
|
32
|
+
message = (error.message || 'An unknown error occurred.');
|
|
33
|
+
} else if (typeof error === 'string') {
|
|
34
|
+
message = (
|
|
35
|
+
error.trim().length > 0
|
|
36
|
+
? error
|
|
37
|
+
: 'An unknown error occurred.'
|
|
38
|
+
);
|
|
39
|
+
} else {
|
|
40
|
+
message = 'An unknown error occurred.';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Get the error code
|
|
44
|
+
const code = (error.code || ReactKitErrorCode.NoCode);
|
|
45
|
+
|
|
46
|
+
// Get the status code
|
|
47
|
+
const status = (error.status || 500);
|
|
48
|
+
|
|
49
|
+
// Respond to user
|
|
50
|
+
res
|
|
51
|
+
// Set the http status code
|
|
52
|
+
.status(status)
|
|
53
|
+
// Send a JSON response
|
|
54
|
+
.json({
|
|
55
|
+
// Error message
|
|
56
|
+
message,
|
|
57
|
+
// Error code
|
|
58
|
+
code,
|
|
59
|
+
// Success = false flag so client can detect server-side errors
|
|
60
|
+
success: false,
|
|
61
|
+
});
|
|
62
|
+
return undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export default handleError;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Send successful API response
|
|
3
|
+
* @author Gabe Abrams
|
|
4
|
+
* @param res express response
|
|
5
|
+
* @param body the body of the response to send to the client
|
|
6
|
+
*/
|
|
7
|
+
const handleSuccess = (res: any, body: any): undefined => {
|
|
8
|
+
// Send a http 200 json response
|
|
9
|
+
res.json({
|
|
10
|
+
// Include the body as a parameter
|
|
11
|
+
body,
|
|
12
|
+
// Success = true flag so client can detect successful responses
|
|
13
|
+
success: true,
|
|
14
|
+
});
|
|
15
|
+
return undefined;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default handleSuccess;
|