Skip to content

Commit

Permalink
feat: add ctx.throw for serverless app (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Sep 1, 2021
1 parent 4d95e5f commit da58bf6
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
22 changes: 22 additions & 0 deletions packages-serverless/faas-typings/typings/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,28 @@ export interface FaaSHTTPContext
* FaaS Cookies Object
*/
cookies: Cookies;

/**
* Throw an error with `msg` and optional `status`
* defaulting to 500. Note that these are user-level
* errors, and the message may be exposed to the client.
*
* this.throw(403)
* this.throw('name required', 400)
* this.throw(400, 'name required')
* this.throw('something exploded')
* this.throw(new Error('invalid'), 400);
* this.throw(400, new Error('invalid'));
*
* See: https://github.com/jshttp/http-errors
*/
throw(
message: string,
code?: number,
properties?: Record<string, unknown>
): never;
throw(status: number): never;
throw(...properties: Array<number | string | Record<string, unknown>>): never;
}

export type FaaSOriginContext = FC.RequestContext | SCF.RequestContext;
1 change: 1 addition & 0 deletions packages-serverless/serverless-http-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"cookies": "^0.8.0",
"encodeurl": "^1.0.2",
"escape-html": "^1.0.3",
"http-errors": "^1.6.3",
"only": "^0.0.2",
"parseurl": "^1.3.3",
"querystring": "^0.2.0",
Expand Down
26 changes: 26 additions & 0 deletions packages-serverless/serverless-http-parser/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FaaSOriginContext } from '@midwayjs/faas-typings';
import * as util from 'util';
import * as createError from 'http-errors';
const Cookies = require('cookies');

const COOKIES = Symbol('context#cookies');
Expand Down Expand Up @@ -156,6 +157,31 @@ export const context = {
return this.request.acceptsLanguages(...args);
},

/**
* Throw an error with `status` (default 500) and
* `msg`. Note that these are user-level
* errors, and the message may be exposed to the client.
*
* this.throw(403)
* this.throw(400, 'name required')
* this.throw('something exploded')
* this.throw(new Error('invalid'))
* this.throw(400, new Error('invalid'))
*
* See: https://github.com/jshttp/http-errors
*
* Note: `status` should only be passed as the first parameter.
*
* @param {String|Number|Error} err, msg or status
* @param {String|Number|Error} [err, msg or status]
* @param {Object} [props]
* @api public
*/

throw(...args) {
throw createError(...args);
},

onerror(err) {
// don't do anything if there is no error.
// this allows you to pass `this.onerror`
Expand Down
15 changes: 15 additions & 0 deletions packages-serverless/serverless-http-parser/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,21 @@ describe('test http parser', () => {
app.onerror(err);
assert(msg === ' Error: mock stack null');
});
it('should test ctx.throw', function () {
const app = new Application();
const req = new HTTPRequest(
require('./resource/scf_apigw.json'),
require('./resource/scf_ctx.json')
);
const res = new HTTPResponse();
const ctx: FaaSHTTPContext = app.createContext(req, res);
try {
ctx.throw(403, 'throw error');
} catch (er) {
// got err
}
assert('run here');
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/oss/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const fs = require('fs');

process.env.MIDWAY_TS_MODE = 'true';
jest.setTimeout(30000);
jest.setTimeout(60000);

const envFile = path.join(__dirname, '.env');
if (!fs.existsSync(envFile)) {
Expand Down
14 changes: 13 additions & 1 deletion packages/oss/test/sts_client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const OSS = require('ali-oss');

export function createSTSClient(accessKeyId, accessKeySecret, stsToken) {
function createClient(accessKeyId, accessKeySecret, stsToken) {
return new OSS({
accessKeyId,
accessKeySecret,
Expand All @@ -11,3 +11,15 @@ export function createSTSClient(accessKeyId, accessKeySecret, stsToken) {
secure: true,
});
}

export function createSTSClient(accessKeyId, accessKeySecret, stsToken) {
let client;
try {
client = createClient(accessKeyId, accessKeySecret, stsToken);
} catch (err) {
console.warn('create sts client error and retry');
client = createClient(accessKeyId, accessKeySecret, stsToken);
}

return client;
}

0 comments on commit da58bf6

Please sign in to comment.