Skip to content

feat(runtime): support better error formatting #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions __tests__/runtime/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Request as ExpressRequest,
Response as ExpressResponse,
} from 'express';
import { Request as MockRequest } from 'jest-express/lib/request';
import { Response as MockResponse } from 'jest-express/lib/response';
import { twiml } from 'twilio';
import { StartCliConfig } from '../../src/config/start';
Expand All @@ -25,12 +26,70 @@ const mockResponse = (new MockResponse() as unknown) as ExpressResponse;
mockResponse.type = jest.fn(() => mockResponse);

describe('handleError function', () => {
test('calls correct response methods', () => {
test('returns string error', () => {
const mockRequest = (new MockRequest() as unknown) as ExpressRequest;
mockRequest['useragent'] = {
isDesktop: true,
isMobile: false,
} as ExpressUseragent.UserAgent;

handleError('string error', mockRequest, mockResponse);

expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith('string error');
});

test('handles objects as error argument', () => {
const mockRequest = (new MockRequest() as unknown) as ExpressRequest;
mockRequest['useragent'] = {
isDesktop: true,
isMobile: false,
} as ExpressUseragent.UserAgent;

handleError({ errorMessage: 'oh no' }, mockRequest, mockResponse);

expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith({ errorMessage: 'oh no' });
});

test('wraps error object for desktop requests', () => {
const mockRequest = (new MockRequest() as unknown) as ExpressRequest;
mockRequest['useragent'] = {
isDesktop: true,
isMobile: false,
} as ExpressUseragent.UserAgent;

const err = new Error('Failed to execute');
handleError(err, (mockResponse as unknown) as ExpressResponse);
handleError(err, mockRequest, mockResponse);
expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith(wrapErrorInHtml(err));
});

test('wraps error object for mobile requests', () => {
const mockRequest = (new MockRequest() as unknown) as ExpressRequest;
mockRequest['useragent'] = {
isDesktop: false,
isMobile: true,
} as ExpressUseragent.UserAgent;

const err = new Error('Failed to execute');
handleError(err, mockRequest, mockResponse);
expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith(wrapErrorInHtml(err));
});

test('returns string version of error for other requests', () => {
const mockRequest = (new MockRequest() as unknown) as ExpressRequest;
mockRequest['useragent'] = {
isDesktop: false,
isMobile: false,
} as ExpressUseragent.UserAgent;

const err = new Error('Failed to execute');
handleError(err, mockRequest, mockResponse);
expect(mockResponse.status).toHaveBeenCalledWith(500);
expect(mockResponse.send).toHaveBeenCalledWith(err.toString());
});
});

describe('constructEvent function', () => {
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"debug": "^3.1.0",
"dotenv": "^6.2.0",
"express": "^4.16.3",
"express-useragent": "^1.0.13",
"fast-redact": "^1.5.0",
"got": "^9.6.0",
"inquirer": "^6.5.0",
Expand Down Expand Up @@ -82,6 +83,7 @@
"@types/common-tags": "^1.8.0",
"@types/debug": "^4.1.4",
"@types/dotenv": "^6.1.1",
"@types/express-useragent": "^0.2.21",
"@types/got": "^9.6.0",
"@types/jest": "^24.0.15",
"@types/listr": "^0.14.0",
Expand Down
17 changes: 13 additions & 4 deletions src/runtime/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,22 @@ export function constructGlobalScope(config: StartCliConfig): void {
}

export function handleError(
err: Error,
err: Error | string | object,
req: ExpressRequest,
res: ExpressResponse,
functionFilePath?: string
) {
res.status(500);
res.type('text/html');
res.send(wrapErrorInHtml(err, functionFilePath));
if (!(err instanceof Error)) {
res.send(err);
} else {
if (req.useragent && (req.useragent.isDesktop || req.useragent.isMobile)) {
res.type('text/html');
res.send(wrapErrorInHtml(err, functionFilePath));
} else {
res.send(err.toString());
}
}
}

export function isTwiml(obj: object): boolean {
Expand Down Expand Up @@ -131,7 +140,7 @@ export function functionToRoute(
) {
debug('Function execution %s finished', req.path);
if (err) {
handleError(err, res, functionFilePath);
handleError(err, req, res, functionFilePath);
return;
}
handleSuccess(responseObject, res);
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import express, {
Request as ExpressRequest,
Response as ExpressResponse,
} from 'express';
import userAgentMiddleware from 'express-useragent';
import nocache from 'nocache';
import { StartCliConfig } from '../config/start';
import { wrapErrorInHtml } from '../utils/error-html';
Expand Down Expand Up @@ -48,6 +49,7 @@ export async function createServer(
debug('Starting server with config: %p', config);

const app = express();
app.use(userAgentMiddleware.express());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/favicon.ico', (req, res) => {
Expand Down