|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +import {Request, Response, NextFunction, Express} from 'express'; |
| 15 | +import {HandlerFunction} from '../functions'; |
| 16 | +import {ILayer} from 'express-serve-static-core'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Common properties that exists on Express object instances. Extracted by calling |
| 20 | + * `Object.getOwnPropertyNames` on an express instance. |
| 21 | + */ |
| 22 | +const COMMON_EXPRESS_OBJECT_PROPERTIES = [ |
| 23 | + '_router', |
| 24 | + 'use', |
| 25 | + 'get', |
| 26 | + 'post', |
| 27 | + 'put', |
| 28 | + 'delete', |
| 29 | +]; |
| 30 | + |
| 31 | +/** The number of parameters on an express error handler. */ |
| 32 | +const EXPRESS_ERROR_HANDLE_PARAM_LENGTH = 4; |
| 33 | + |
| 34 | +/** A express app error handle. */ |
| 35 | +interface ErrorHandle { |
| 36 | + (err: Error, req: Request, res: Response, next: NextFunction): any; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Express middleware to propagate framework errors to the user function error handle. |
| 41 | + * This enables users to handle framework errors that would otherwise be handled by the |
| 42 | + * default Express error handle. If the user function doesn't have an error handle, |
| 43 | + * it falls back to the default Express error handle. |
| 44 | + * @param userFunction - User handler function |
| 45 | + */ |
| 46 | +export const createPropagateErrorToClientErrorHandleMiddleware = ( |
| 47 | + userFunction: HandlerFunction |
| 48 | +): ErrorHandle => { |
| 49 | + const userFunctionErrorHandle = |
| 50 | + getFirstUserFunctionErrorHandleMiddleware(userFunction); |
| 51 | + |
| 52 | + return function ( |
| 53 | + err: Error, |
| 54 | + req: Request, |
| 55 | + res: Response, |
| 56 | + next: NextFunction |
| 57 | + ) { |
| 58 | + // Propagate error to user function error handle. |
| 59 | + if (userFunctionErrorHandle) { |
| 60 | + return userFunctionErrorHandle(err, req, res, next); |
| 61 | + } |
| 62 | + |
| 63 | + // Propagate error to default Express error handle. |
| 64 | + return next(); |
| 65 | + }; |
| 66 | +}; |
| 67 | + |
| 68 | +/** |
| 69 | + * Returns the first user handler function defined error handle, if available. |
| 70 | + * @param userFunction - User handler function |
| 71 | + */ |
| 72 | +const getFirstUserFunctionErrorHandleMiddleware = ( |
| 73 | + userFunction: HandlerFunction |
| 74 | +): ErrorHandle | null => { |
| 75 | + if (!isExpressApp(userFunction)) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + const middlewares: ILayer[] = (userFunction as Express)._router.stack; |
| 80 | + for (const middleware of middlewares) { |
| 81 | + if ( |
| 82 | + middleware.handle && |
| 83 | + middleware.handle.length === EXPRESS_ERROR_HANDLE_PARAM_LENGTH |
| 84 | + ) { |
| 85 | + return middleware.handle as unknown as ErrorHandle; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return null; |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * Returns if the user function contains common properties of an Express app. |
| 94 | + * @param userFunction |
| 95 | + */ |
| 96 | +const isExpressApp = (userFunction: HandlerFunction): boolean => { |
| 97 | + const userFunctionProperties = Object.getOwnPropertyNames(userFunction); |
| 98 | + return COMMON_EXPRESS_OBJECT_PROPERTIES.every(prop => |
| 99 | + userFunctionProperties.includes(prop) |
| 100 | + ); |
| 101 | +}; |
0 commit comments