@webiny/handler 0.0.0-unstable.78f581c1d2 → 0.0.0-unstable.8acc9e8892

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/Context.d.ts CHANGED
@@ -1,13 +1,23 @@
1
- import { Context as BaseContext, ContextParams as BaseContextParams } from "@webiny/api";
2
- import { Context as BaseContextType } from "./types";
1
+ import { Context as BaseContext, ContextParams as BaseContextParams, ContextPlugin as BaseContextPlugin, ContextPluginCallable as BaseContextPluginCallable } from "@webiny/api";
2
+ import { Context as ContextInterface } from "./types";
3
3
  export interface ContextParams extends BaseContextParams {
4
- server: BaseContextType["server"];
5
- routes: BaseContextType["routes"];
4
+ server: ContextInterface["server"];
5
+ routes: ContextInterface["routes"];
6
6
  }
7
- export declare class Context extends BaseContext implements BaseContextType {
8
- readonly server: BaseContextType["server"];
9
- readonly routes: BaseContextType["routes"];
10
- handlerClient: BaseContextType["handlerClient"];
11
- request: BaseContextType["request"];
7
+ export declare class Context extends BaseContext implements ContextInterface {
8
+ readonly server: ContextInterface["server"];
9
+ readonly routes: ContextInterface["routes"];
10
+ handlerClient: ContextInterface["handlerClient"];
11
+ request: ContextInterface["request"];
12
+ reply: ContextInterface["reply"];
12
13
  constructor(params: ContextParams);
13
14
  }
15
+ /**
16
+ * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
17
+ *
18
+ * This can be removed when we introduce the type augmentation.
19
+ */
20
+ export declare type ContextPluginCallable<T extends ContextInterface = ContextInterface> = BaseContextPluginCallable<T>;
21
+ export declare class ContextPlugin<T extends ContextInterface = ContextInterface> extends BaseContextPlugin<T> {
22
+ }
23
+ export declare const createContextPlugin: <T extends ContextInterface = ContextInterface>(callable: ContextPluginCallable<T>) => BaseContextPlugin<T>;
package/Context.js CHANGED
@@ -1,29 +1,40 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
- exports.Context = void 0;
9
-
7
+ exports.createContextPlugin = exports.ContextPlugin = exports.Context = void 0;
10
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
9
  var _api = require("@webiny/api");
13
-
14
10
  class Context extends _api.Context {
15
11
  // @ts-ignore
12
+
16
13
  // @ts-ignore
14
+
15
+ // @ts-ignore
16
+
17
17
  constructor(params) {
18
18
  super(params);
19
19
  (0, _defineProperty2.default)(this, "server", void 0);
20
20
  (0, _defineProperty2.default)(this, "routes", void 0);
21
21
  (0, _defineProperty2.default)(this, "handlerClient", void 0);
22
22
  (0, _defineProperty2.default)(this, "request", void 0);
23
+ (0, _defineProperty2.default)(this, "reply", void 0);
23
24
  this.server = params.server;
24
25
  this.routes = params.routes;
25
26
  }
26
-
27
27
  }
28
28
 
29
- exports.Context = Context;
29
+ /**
30
+ * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.
31
+ *
32
+ * This can be removed when we introduce the type augmentation.
33
+ */
34
+ exports.Context = Context;
35
+ class ContextPlugin extends _api.ContextPlugin {}
36
+ exports.ContextPlugin = ContextPlugin;
37
+ const createContextPlugin = callable => {
38
+ return (0, _api.createContextPlugin)(callable);
39
+ };
40
+ exports.createContextPlugin = createContextPlugin;
package/Context.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["Context","BaseContext","constructor","params","server","routes"],"sources":["Context.ts"],"sourcesContent":["import { Context as BaseContext, ContextParams as BaseContextParams } from \"@webiny/api\";\nimport { Context as BaseContextType } from \"~/types\";\n\nexport interface ContextParams extends BaseContextParams {\n server: BaseContextType[\"server\"];\n routes: BaseContextType[\"routes\"];\n}\n\nexport class Context extends BaseContext implements BaseContextType {\n public readonly server: BaseContextType[\"server\"];\n public readonly routes: BaseContextType[\"routes\"];\n // @ts-ignore\n public handlerClient: BaseContextType[\"handlerClient\"];\n // @ts-ignore\n public request: BaseContextType[\"request\"];\n\n public constructor(params: ContextParams) {\n super(params);\n this.server = params.server;\n this.routes = params.routes;\n }\n}\n"],"mappings":";;;;;;;;;;;AAAA;;AAQO,MAAMA,OAAN,SAAsBC,YAAtB,CAA6D;EAGhE;EAEA;EAGOC,WAAW,CAACC,MAAD,EAAwB;IACtC,MAAMA,MAAN;IADsC;IAAA;IAAA;IAAA;IAEtC,KAAKC,MAAL,GAAcD,MAAM,CAACC,MAArB;IACA,KAAKC,MAAL,GAAcF,MAAM,CAACE,MAArB;EACH;;AAZ+D"}
1
+ {"version":3,"names":["Context","BaseContext","constructor","params","server","routes","ContextPlugin","BaseContextPlugin","createContextPlugin","callable","baseCreateContextPlugin"],"sources":["Context.ts"],"sourcesContent":["import {\n Context as BaseContext,\n ContextParams as BaseContextParams,\n ContextPlugin as BaseContextPlugin,\n ContextPluginCallable as BaseContextPluginCallable,\n createContextPlugin as baseCreateContextPlugin\n} from \"@webiny/api\";\nimport { Context as ContextInterface } from \"~/types\";\n\nexport interface ContextParams extends BaseContextParams {\n server: ContextInterface[\"server\"];\n routes: ContextInterface[\"routes\"];\n}\n\nexport class Context extends BaseContext implements ContextInterface {\n public readonly server: ContextInterface[\"server\"];\n public readonly routes: ContextInterface[\"routes\"];\n // @ts-ignore\n public handlerClient: ContextInterface[\"handlerClient\"];\n // @ts-ignore\n public request: ContextInterface[\"request\"];\n // @ts-ignore\n public reply: ContextInterface[\"reply\"];\n\n public constructor(params: ContextParams) {\n super(params);\n this.server = params.server;\n this.routes = params.routes;\n }\n}\n\n/**\n * We need to extend and reexport the ContextPlugin, ContextPluginCallable and createContextPlugin to support extended context.\n *\n * This can be removed when we introduce the type augmentation.\n */\nexport type ContextPluginCallable<T extends ContextInterface = ContextInterface> =\n BaseContextPluginCallable<T>;\n\nexport class ContextPlugin<\n T extends ContextInterface = ContextInterface\n> extends BaseContextPlugin<T> {}\n\nexport const createContextPlugin = <T extends ContextInterface = ContextInterface>(\n callable: ContextPluginCallable<T>\n) => {\n return baseCreateContextPlugin<T>(callable);\n};\n"],"mappings":";;;;;;;;AAAA;AAcO,MAAMA,OAAO,SAASC,YAAW,CAA6B;EAGjE;;EAEA;;EAEA;;EAGOC,WAAW,CAACC,MAAqB,EAAE;IACtC,KAAK,CAACA,MAAM,CAAC;IAAC;IAAA;IAAA;IAAA;IAAA;IACd,IAAI,CAACC,MAAM,GAAGD,MAAM,CAACC,MAAM;IAC3B,IAAI,CAACC,MAAM,GAAGF,MAAM,CAACE,MAAM;EAC/B;AACJ;;AAEA;AACA;AACA;AACA;AACA;AAJA;AAQO,MAAMC,aAAa,SAEhBC,kBAAiB,CAAI;AAAE;AAE1B,MAAMC,mBAAmB,GAC5BC,QAAkC,IACjC;EACD,OAAO,IAAAC,wBAAuB,EAAID,QAAQ,CAAC;AAC/C,CAAC;AAAC"}
package/fastify.js CHANGED
@@ -1,38 +1,26 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.createHandler = void 0;
9
-
10
8
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
-
12
9
  var _fastify = _interopRequireDefault(require("fastify"));
13
-
14
10
  var _utils = require("@webiny/utils");
15
-
16
11
  var _Context = require("./Context");
17
-
18
12
  var _error = _interopRequireDefault(require("@webiny/error"));
19
-
20
13
  var _RoutePlugin = require("./plugins/RoutePlugin");
21
-
22
14
  var _handlerClient = require("@webiny/handler-client");
23
-
24
15
  var _cookie = _interopRequireDefault(require("@fastify/cookie"));
25
-
16
+ var _compress = _interopRequireDefault(require("@fastify/compress"));
26
17
  var _middleware = require("./middleware");
27
-
28
18
  var _api = require("@webiny/api");
29
-
30
19
  var _BeforeHandlerPlugin = require("./plugins/BeforeHandlerPlugin");
31
-
32
20
  var _HandlerResultPlugin = require("./plugins/HandlerResultPlugin");
33
-
34
21
  var _HandlerErrorPlugin = require("./plugins/HandlerErrorPlugin");
35
-
22
+ var _ModifyFastifyPlugin = require("./plugins/ModifyFastifyPlugin");
23
+ var _HandlerOnRequestPlugin = require("./plugins/HandlerOnRequestPlugin");
36
24
  const DEFAULT_HEADERS = (0, _objectSpread2.default)({
37
25
  "Cache-Control": "no-store",
38
26
  "Content-Type": "application/json; charset=utf-8",
@@ -40,38 +28,48 @@ const DEFAULT_HEADERS = (0, _objectSpread2.default)({
40
28
  "Access-Control-Allow-Headers": "*",
41
29
  "Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE,PUT,PATCH"
42
30
  }, (0, _utils.getWebinyVersionHeaders)());
43
-
44
31
  const getDefaultHeaders = routes => {
45
32
  /**
46
33
  * If we are accepting all headers, just output that one.
47
34
  */
48
35
  const keys = Object.keys(routes);
49
36
  const all = keys.every(key => routes[key].length > 0);
50
-
51
37
  if (all) {
52
38
  return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
53
39
  "Access-Control-Allow-Methods": "*"
54
40
  });
55
41
  }
56
-
57
42
  return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, DEFAULT_HEADERS), {}, {
58
- "Access-Control-Allow-Methods": keys.filter(key => {
59
- const type = key;
60
-
43
+ "Access-Control-Allow-Methods": keys.filter(type => {
61
44
  if (!routes[type] || Array.isArray(routes[type]) === false) {
62
45
  return false;
63
46
  }
64
-
65
47
  return routes[type].length > 0;
66
48
  }).sort().join(",")
67
49
  });
68
50
  };
69
-
51
+ const stringifyError = error => {
52
+ var _error$constructor;
53
+ const {
54
+ name,
55
+ message,
56
+ code,
57
+ stack,
58
+ data
59
+ } = error;
60
+ return JSON.stringify((0, _objectSpread2.default)((0, _objectSpread2.default)({}, error), {}, {
61
+ constructorName: ((_error$constructor = error.constructor) === null || _error$constructor === void 0 ? void 0 : _error$constructor.name) || "UnknownError",
62
+ name: name || "No error name",
63
+ message: message || "No error message",
64
+ code: code || "NO_CODE",
65
+ data,
66
+ stack: process.env.DEBUG === "true" ? stack : "Turn on the debug flag to see the stack."
67
+ }));
68
+ };
70
69
  const OPTIONS_HEADERS = {
71
70
  "Access-Control-Max-Age": "86400",
72
71
  "Cache-Control": "public, max-age=86400"
73
72
  };
74
-
75
73
  const createHandler = params => {
76
74
  const definedRoutes = {
77
75
  POST: [],
@@ -91,18 +89,17 @@ const createHandler = params => {
91
89
  TRACE: [],
92
90
  UNLOCK: []
93
91
  };
94
-
95
92
  const throwOnDefinedRoute = (type, path, options) => {
96
93
  if (type === "ALL") {
97
- const all = Object.keys(definedRoutes).some(key => {
94
+ const all = Object.keys(definedRoutes).find(key => {
98
95
  const routes = definedRoutes[key];
99
96
  return routes.includes(path);
100
97
  });
101
-
102
98
  if (!all) {
103
99
  return;
104
100
  }
105
-
101
+ console.error(`Error while registering onAll route. One of the routes is already defined.`);
102
+ console.error(JSON.stringify(all));
106
103
  throw new _error.default(`You cannot override a route with onAll() method, please remove unnecessary route from the system.`, "OVERRIDE_ROUTE_ERROR", {
107
104
  type,
108
105
  path
@@ -112,59 +109,66 @@ const createHandler = params => {
112
109
  } else if ((options === null || options === void 0 ? void 0 : options.override) === true) {
113
110
  return;
114
111
  }
115
-
112
+ console.error(`Error while trying to override route: [${type}] ${path}`);
116
113
  throw new _error.default(`When you are trying to override existing route, you must send "override" parameter when adding that route.`, "OVERRIDE_ROUTE_ERROR", {
117
114
  type,
118
115
  path
119
116
  });
120
117
  };
121
-
122
- const addDefinedRoute = (inputType, path) => {
123
- const type = inputType.toUpperCase();
124
-
118
+ const addDefinedRoute = (type, path) => {
125
119
  if (!definedRoutes[type]) {
126
120
  return;
127
121
  } else if (definedRoutes[type].includes(path)) {
128
122
  return;
129
123
  }
130
-
131
124
  definedRoutes[type].push(path);
132
125
  };
133
126
  /**
134
127
  * We must attach the server to our internal context if we want to have it accessible.
135
128
  */
136
-
137
-
138
129
  const app = (0, _fastify.default)((0, _objectSpread2.default)({}, params.options || {}));
139
130
  /**
140
131
  * We need to register routes in our system so we can output headers later on and dissallow overriding routes.
141
132
  */
142
-
143
133
  app.addHook("onRoute", route => {
144
134
  const method = route.method;
145
-
146
135
  if (Array.isArray(method)) {
147
136
  for (const m of method) {
148
137
  addDefinedRoute(m, route.path);
149
138
  }
150
-
151
139
  return;
152
140
  }
153
-
154
141
  addDefinedRoute(method, route.path);
155
142
  });
156
143
  /**
144
+ * ############################
145
+ * Register the Fastify plugins.
146
+ */
147
+ /**
148
+ * Package @fastify/cookie
157
149
  *
150
+ * https://github.com/fastify/fastify-cookie
158
151
  */
159
-
160
152
  app.register(_cookie.default, {
161
153
  parseOptions: {} // options for parsing cookies
162
-
154
+ });
155
+ /**
156
+ * Package @fastify/compress
157
+ *
158
+ * https://github.com/fastify/fastify-compress
159
+ */
160
+ app.register(_compress.default, {
161
+ global: true,
162
+ threshold: 1024,
163
+ onUnsupportedEncoding: (encoding, _, reply) => {
164
+ reply.code(406);
165
+ return `We do not support the ${encoding} encoding.`;
166
+ },
167
+ inflateIfDeflated: true
163
168
  });
164
169
  /**
165
170
  * Route helpers - mostly for users.
166
171
  */
167
-
168
172
  const routes = {
169
173
  defined: definedRoutes,
170
174
  onPost: (path, handler, options) => {
@@ -200,26 +204,33 @@ const createHandler = params => {
200
204
  app.head(path, handler);
201
205
  }
202
206
  };
203
- const context = new _Context.Context({
204
- plugins: [
205
- /**
206
- * We must have handlerClient by default.
207
- * And it must be one of the first context plugins applied.
208
- */
209
- (0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
207
+ let context;
208
+ try {
209
+ context = new _Context.Context({
210
+ plugins: [
211
+ /**
212
+ * We must have handlerClient by default.
213
+ * And it must be one of the first context plugins applied.
214
+ */
215
+ (0, _handlerClient.createHandlerClient)(), ...(params.plugins || [])],
216
+ /**
217
+ * Inserted via webpack on build time.
218
+ */
219
+ WEBINY_VERSION: process.env.WEBINY_VERSION,
220
+ server: app,
221
+ routes
222
+ });
223
+ } catch (ex) {
224
+ console.error(`Error while constructing the Context.`);
225
+ console.error(stringifyError(ex));
226
+ throw ex;
227
+ }
210
228
 
211
- /**
212
- * Inserted via webpack on build time.
213
- */
214
- WEBINY_VERSION: process.env.WEBINY_VERSION,
215
- server: app,
216
- routes
217
- });
218
229
  /**
219
- * We are attaching our custom context to webiny variable on the fastify app so it is accessible everywhere
230
+ * We are attaching our custom context to webiny variable on the fastify app, so it is accessible everywhere.
220
231
  */
221
-
222
232
  app.decorate("webiny", context);
233
+
223
234
  /**
224
235
  * We have few types of triggers:
225
236
  * * Events - EventPlugin
@@ -227,93 +238,190 @@ const createHandler = params => {
227
238
  *
228
239
  * Routes are registered in fastify but events must be handled in package which implements cloud specific methods.
229
240
  */
230
-
231
241
  const routePlugins = app.webiny.plugins.byType(_RoutePlugin.RoutePlugin.type);
242
+
232
243
  /**
233
244
  * Add routes to the system.
234
245
  */
235
-
236
- for (const plugin of routePlugins) {
237
- plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
238
- context: app.webiny
239
- }));
246
+ let routePluginName;
247
+ try {
248
+ for (const plugin of routePlugins) {
249
+ routePluginName = plugin.name;
250
+ plugin.cb((0, _objectSpread2.default)((0, _objectSpread2.default)({}, app.webiny.routes), {}, {
251
+ context: app.webiny
252
+ }));
253
+ }
254
+ } catch (ex) {
255
+ console.error(`Error while running the "RoutePlugin" ${routePluginName ? `(${routePluginName})` : ""} plugin in the beginning of the "createHandler" callable.`);
256
+ console.error(stringifyError(ex));
257
+ throw ex;
240
258
  }
259
+
241
260
  /**
242
261
  * On every request we add default headers, which can be changed later.
243
262
  * Also, if it is an options request, we skip everything after this hook and output options headers.
244
263
  */
245
-
246
-
247
264
  app.addHook("onRequest", async (request, reply) => {
265
+ /**
266
+ * Our default headers are always set. Users can override them.
267
+ */
248
268
  const defaultHeaders = getDefaultHeaders(definedRoutes);
249
269
  reply.headers(defaultHeaders);
250
-
270
+ /**
271
+ * Users can define their own custom handlers for the onRequest event - so let's run them first.
272
+ */
273
+ const plugins = app.webiny.plugins.byType(_HandlerOnRequestPlugin.HandlerOnRequestPlugin.type);
274
+ let name;
275
+ try {
276
+ for (const plugin of plugins) {
277
+ name = plugin.name;
278
+ const result = await plugin.exec(request, reply);
279
+ if (result === false) {
280
+ return;
281
+ }
282
+ }
283
+ } catch (ex) {
284
+ console.error(`Error while running the "HandlerOnRequestPlugin" ${name ? `(${name})` : ""} plugin in the onRequest hook.`);
285
+ console.error(stringifyError(ex));
286
+ throw ex;
287
+ }
288
+ /**
289
+ * When we receive the OPTIONS request, we end it before it goes any further as there is no need for anything to run after this - at least for our use cases.
290
+ *
291
+ * Users can prevent this by creating their own HandlerOnRequestPlugin and returning false as the result of the callable.
292
+ */
251
293
  if (request.method !== "OPTIONS") {
252
294
  return;
253
295
  }
254
-
255
- const raw = reply.code(204).hijack().raw;
256
- const headers = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS);
257
-
258
- for (const key in headers) {
259
- raw.setHeader(key, headers[key]);
296
+ if (reply.sent) {
297
+ /**
298
+ * At this point throwing an exception will not do anything with the response. So just log it.
299
+ */
300
+ console.error(JSON.stringify({
301
+ message: `Output was already sent. Please check custom plugins of type "HandlerOnRequestPlugin".`,
302
+ explanation: "This error can happen if the user plugin ended the reply, but did not return false as response."
303
+ }));
304
+ return;
260
305
  }
261
-
262
- raw.end("");
306
+ reply.headers((0, _objectSpread2.default)((0, _objectSpread2.default)({}, defaultHeaders), OPTIONS_HEADERS)).code(204).send("").hijack();
263
307
  });
264
- app.addHook("preParsing", async request => {
308
+ app.addHook("preParsing", async (request, reply) => {
265
309
  app.webiny.request = request;
310
+ app.webiny.reply = reply;
266
311
  const plugins = app.webiny.plugins.byType(_api.ContextPlugin.type);
267
-
268
- for (const plugin of plugins) {
269
- await plugin.apply(app.webiny);
312
+ let name;
313
+ try {
314
+ for (const plugin of plugins) {
315
+ name = plugin.name;
316
+ await plugin.apply(app.webiny);
317
+ }
318
+ } catch (ex) {
319
+ console.error(`Error while running the "ContextPlugin" ${name ? `(${name})` : ""} plugin in the preParsing hook.`);
320
+ console.error(stringifyError(ex));
321
+ throw ex;
270
322
  }
271
323
  });
272
324
  /**
273
325
  *
274
326
  */
275
-
276
327
  app.addHook("preHandler", async () => {
277
328
  const plugins = app.webiny.plugins.byType(_BeforeHandlerPlugin.BeforeHandlerPlugin.type);
278
-
279
- for (const plugin of plugins) {
280
- await plugin.apply(app.webiny);
329
+ let name;
330
+ try {
331
+ for (const plugin of plugins) {
332
+ name = plugin.name;
333
+ await plugin.apply(app.webiny);
334
+ }
335
+ } catch (ex) {
336
+ console.error(`Error while running the "BeforeHandlerPlugin" ${name ? `(${name})` : ""} plugin in the preHandler hook.`);
337
+ console.error(stringifyError(ex));
338
+ throw ex;
281
339
  }
282
340
  });
341
+
283
342
  /**
284
343
  *
285
344
  */
286
-
287
345
  const preSerialization = async (_, __, payload) => {
288
346
  const plugins = app.webiny.plugins.byType(_HandlerResultPlugin.HandlerResultPlugin.type);
289
-
290
- for (const plugin of plugins) {
291
- await plugin.handle(app.webiny, payload);
347
+ let name;
348
+ try {
349
+ for (const plugin of plugins) {
350
+ name = plugin.name;
351
+ await plugin.handle(app.webiny, payload);
352
+ }
353
+ } catch (ex) {
354
+ console.error(`Error while running the "HandlerResultPlugin" ${name ? `(${name})` : ""} plugin in the preSerialization hook.`);
355
+ console.error(stringifyError(ex));
356
+ throw ex;
292
357
  }
293
-
294
358
  return payload;
295
359
  };
296
-
297
360
  app.addHook("preSerialization", preSerialization);
361
+ app.setErrorHandler(async (error, request, reply) => {
362
+ return reply.status(500).headers({
363
+ "Cache-Control": "no-store"
364
+ }).send(
365
+ /**
366
+ * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
367
+ */
368
+ JSON.stringify({
369
+ message: error.message,
370
+ code: error.code,
371
+ data: error.data
372
+ }));
373
+ });
298
374
  app.addHook("onError", async (_, reply, error) => {
299
- const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type); // Log error to cloud, as these can be extremely annoying to debug!
300
-
301
- console.log("@webiny/handler");
302
- console.log(JSON.stringify((0, _objectSpread2.default)((0, _objectSpread2.default)({}, error || {}), {}, {
303
- message: error === null || error === void 0 ? void 0 : error.message,
304
- code: error === null || error === void 0 ? void 0 : error.code
305
- })));
375
+ const plugins = app.webiny.plugins.byType(_HandlerErrorPlugin.HandlerErrorPlugin.type);
376
+ /**
377
+ * Log error to cloud, as these can be extremely annoying to debug!
378
+ */
379
+ console.error("@webiny/handler");
380
+ console.error(stringifyError(error));
381
+ reply.status(500).headers({
382
+ "Cache-Control": "no-store"
383
+ }).send(
384
+ /**
385
+ * When we are sending the error in the response, we cannot send the whole error object, as it might contain some sensitive data.
386
+ */
387
+ JSON.stringify({
388
+ message: error.message,
389
+ code: error.code,
390
+ data: error.data
391
+ }));
306
392
  const handler = (0, _middleware.middleware)(plugins.map(pl => {
307
393
  return (context, error, next) => {
308
394
  return pl.handle(context, error, next);
309
395
  };
310
396
  }));
311
397
  await handler(app.webiny, error);
312
- return reply.headers({
313
- "Cache-Control": "no-store"
314
- }).status(500);
398
+ return reply;
399
+ });
400
+ /**
401
+ * We need to output the benchmark results at the end of the request in both response and timeout cases
402
+ */
403
+ app.addHook("onResponse", async () => {
404
+ await context.benchmark.output();
405
+ });
406
+ app.addHook("onTimeout", async () => {
407
+ await context.benchmark.output();
315
408
  });
409
+
410
+ /**
411
+ * With these plugins we give users possibility to do anything they want on our fastify instance.
412
+ */
413
+ const modifyPlugins = app.webiny.plugins.byType(_ModifyFastifyPlugin.ModifyFastifyPlugin.type);
414
+ let modifyFastifyPluginName;
415
+ try {
416
+ for (const plugin of modifyPlugins) {
417
+ modifyFastifyPluginName = plugin.name;
418
+ plugin.modify(app);
419
+ }
420
+ } catch (ex) {
421
+ console.error(`Error while running the "ModifyFastifyPlugin" ${modifyFastifyPluginName ? `(${modifyFastifyPluginName})` : ""} plugin in the end of the "createHandler" callable.`);
422
+ console.error(stringifyError(ex));
423
+ throw ex;
424
+ }
316
425
  return app;
317
426
  };
318
-
319
427
  exports.createHandler = createHandler;