Skip to content

Commit

Permalink
fix: hide real error when user code throw error (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Jul 1, 2021
1 parent 950ea4f commit e728b0b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.6.0
with:
mongodb-version: 4.2

- run: npm install && npm install codecov
- run: npm run bootstrap
- run: npm run build --if-present
Expand Down
16 changes: 15 additions & 1 deletion packages-serverless/serverless-fc-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,26 @@ export class FCRuntime extends ServerlessLightRuntime {
// format context
const newCtx = {
logger: context.logger || console,
originEvent: event,
originContext: context,
};
// 其他事件场景
return this.invokeHandlerWrapper(newCtx, async () => {
args[0] = newCtx;
return handler.apply(handler, args);

try {
if (!handler) {
return await this.defaultInvokeHandler(...args);
} else {
return await handler.apply(handler, args);
}
} catch (err) {
if (isOutputError()) {
throw err;
} else {
throw new Error('Internal Server Error');
}
}
});
}

Expand Down
48 changes: 46 additions & 2 deletions packages-serverless/serverless-fc-starter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ function send(request: events.EventEmitter, data: string) {
}

describe('/test/index.test.ts', () => {

afterEach(() => {
jest.restoreAllMocks()
});

describe('wrapper normal event', () => {
it('should wrap in init function', async () => {
const handle = asyncWrapper(async context => {
Expand All @@ -77,6 +82,38 @@ describe('/test/index.test.ts', () => {
const res: any = await test(handle).run({ data: 1 }, {});
assert.equal(res, 1);
});

it('should wrap with event and throw error', async () => {
const runtime = await start({});

let err;

try {
await runtime.asyncEvent(async (ctx, event) => {
throw new Error('abc');
})();
} catch (error) {
err = error;
}
expect(err.message).toEqual('Internal Server Error');
});

it('should wrap with event and throw real error with env', async () => {
process.env.SERVERLESS_OUTPUT_ERROR_STACK = 'true';
const runtime = await start({});

let err;

try {
await runtime.asyncEvent(async (ctx, event) => {
throw new Error('abc');
})();
} catch (error) {
err = error;
}
expect(err.message).toEqual('abc');
process.env.SERVERLESS_OUTPUT_ERROR_STACK = '';
});
});

describe('wrapper web event fc', () => {
Expand Down Expand Up @@ -232,7 +269,7 @@ describe('/test/index.test.ts', () => {
}

assert.ok(err);
assert.equal(err.message, 'ooops!');
assert.equal(err.message, 'Internal Server Error');
});

it('should ok with asyncWrap when not async functions', async () => {
Expand Down Expand Up @@ -500,7 +537,14 @@ describe('/test/index.test.ts', () => {
});
});

describe('test entry param', () => {
describe('test new property', () => {

it('should get app', async () => {
const runtime = await start();
const app = runtime.getApplication();
expect(app.use).toBeDefined();
});

it('should get function name and service name from init context', async () => {
const runtime = await start({
initContext:{
Expand Down
16 changes: 13 additions & 3 deletions packages-serverless/serverless-scf-starter/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,25 @@ export class SCFRuntime extends ServerlessLightRuntime {
// format context
const newCtx = {
logger: console,
originEvent: event,
originContext: context,
};
const args = [newCtx, event];
// 其他事件场景
return this.invokeHandlerWrapper(context, async () => {
if (!handler) {
return this.defaultInvokeHandler(...args);
try {
if (!handler) {
return await this.defaultInvokeHandler(...args);
} else {
return await handler.apply(handler, args);
}
} catch (err) {
if (isOutputError()) {
throw err;
} else {
throw new Error('Internal Server Error');
}
}
return handler.apply(handler, args);
});
}

Expand Down
43 changes: 43 additions & 0 deletions packages-serverless/serverless-scf-starter/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ function test(handler) {
}

describe('/test/index.test.ts', () => {

afterEach(() => {
jest.restoreAllMocks()
});

describe('wrapper normal event', () => {
it('should wrap in init function', async () => {
const handle = asyncWrapper(async context => {
Expand All @@ -55,6 +60,38 @@ describe('/test/index.test.ts', () => {
const res: any = await test(handle).run({ data: 1 }, {});
assert.equal(res, 1);
});

it('should wrap with event and throw error', async () => {
const runtime = await start({});

let err;

try {
await runtime.asyncEvent(async (ctx, event) => {
throw new Error('abc');
})();
} catch (error) {
err = error;
}
expect(err.message).toEqual('Internal Server Error');
});

it('should wrap with event and throw real error with env', async () => {
process.env.SERVERLESS_OUTPUT_ERROR_STACK = 'true';
const runtime = await start({});

let err;

try {
await runtime.asyncEvent(async (ctx, event) => {
throw new Error('abc');
})();
} catch (error) {
err = error;
}
expect(err.message).toEqual('abc');
process.env.SERVERLESS_OUTPUT_ERROR_STACK = '';
});
});

describe('wrapper web event scf', () => {
Expand Down Expand Up @@ -436,5 +473,11 @@ describe('/test/index.test.ts', () => {
expect(runtime.getFunctionServiceName()).toEqual('');
mm.restore();
});

it('should get app', async () => {
const runtime = await start();
const app = runtime.getApplication();
expect(app.use).toBeDefined();
});
})
});
17 changes: 9 additions & 8 deletions packages/typegoose/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { createApp, close } from '@midwayjs/mock';
import { Framework } from '@midwayjs/koa'
import { join } from 'path';
import { closeMongoServer, createMongoServer } from './util';
// import { closeMongoServer, createMongoServer } from './util';

describe('/test/index.test.ts', () => {

beforeAll(async () => {
await createMongoServer();
})

afterAll(async () => {
await closeMongoServer();
})
// beforeAll(async () => {
// await createMongoServer();
// })
//
// afterAll(async () => {
// await closeMongoServer();
// })

it('should connect mongodb', async () => {
process.env.MONGO_URI = 'mongodb://localhost:27017';
let app = await createApp(join(__dirname, 'fixtures', 'base-app'), {}, Framework);
await close(app);
});
Expand Down

0 comments on commit e728b0b

Please sign in to comment.