Skip to content

Commit 1be418c

Browse files
ofrobotsJustinBeckwith
authored andcommitted
refactor: cleanup types for reportManualError (#310)
1 parent 1ddab28 commit 1be418c

2 files changed

Lines changed: 45 additions & 19 deletions

File tree

handwritten/nodejs-error-reporting/src/interfaces/manual.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import {Request} from '../request-extractors/manual';
2727
export type Callback =
2828
(err: Error|null, response: http.ServerResponse|null, body: {}) => void;
2929

30+
// tslint:disable-next-line:no-any
31+
type AnyError = any;
32+
3033
/**
3134
* The handler setup function serves to produce a bound instance of the
3235
* reportManualError function with no bound context, a bound first arugment
@@ -60,11 +63,25 @@ export function handlerSetup(
6063
* @returns {ErrorMessage} - returns the error message created through with
6164
* the parameters given.
6265
*/
63-
// the `err` argument can be anything, including `null` and `undefined`
66+
function reportManualError(err: AnyError): ErrorMessage;
67+
function reportManualError(err: AnyError, request: Request): ErrorMessage;
68+
function reportManualError(
69+
err: AnyError, additionalMessage: string): ErrorMessage;
70+
function reportManualError(err: AnyError, callback: Callback): ErrorMessage;
71+
function reportManualError(
72+
err: AnyError, request: Request, callback: Callback): ErrorMessage;
73+
function reportManualError(
74+
err: AnyError, request: Request, additionalMessage: string): ErrorMessage;
75+
function reportManualError(
76+
err: AnyError, additionalMessage: string,
77+
callback: Callback): ErrorMessage;
78+
function reportManualError(
79+
err: AnyError, request: Request, additionalMessage: string,
80+
callback: Callback): ErrorMessage;
6481
function reportManualError(
65-
err: any, // tslint:disable-line:no-any
66-
request?: Request|Callback|string, additionalMessage?: Callback|string|{},
67-
callback?: Callback|{}|string) {
82+
err: AnyError, request?: Request|Callback|string,
83+
additionalMessage?: Callback|string|{},
84+
callback?: Callback|{}|string): ErrorMessage {
6885
let em;
6986
if (is.string(request)) {
7087
// no request given

handwritten/nodejs-error-reporting/test/unit/interfaces/manual.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const config = new Configuration({});
2727
import {ErrorMessage} from '../../../src/classes/error-message';
2828
import {RequestHandler} from '../../../src/google-apis/auth-client';
2929
import {RequestInformationContainer} from '../../../src/classes/request-information-container';
30+
import {Request} from '../../../src/request-extractors/manual';
3031

3132
describe('Manual handler', () => {
3233
// nock.disableNetConnect();
@@ -117,17 +118,21 @@ describe('Manual handler', () => {
117118
assert.strictEqual(r.context.httpRequest.method, 'TACKEY');
118119
});
119120
it('Should ignore arguments', done => {
120-
const r = report('hockey', () => {
121-
done();
122-
}, 'field');
121+
const r = report(
122+
'hockey', (() => {
123+
done();
124+
}) as unknown as string,
125+
'field' as unknown as manual.Callback);
123126
assert(
124127
r.message.match('hockey') && !r.message.match('field'),
125128
'string after callback should be ignored');
126129
});
127130
it('Should ignore arguments', done => {
128-
const r = report('passkey', () => {
129-
done();
130-
}, {method: 'HONK'});
131+
const r = report(
132+
'passkey', (() => {
133+
done();
134+
}) as unknown as string,
135+
{method: 'HONK'} as unknown as manual.Callback);
131136
assert.notEqual(r.context.httpRequest.method, 'HONK');
132137
});
133138
it('Should allow null arguments as placeholders', done => {
@@ -137,21 +142,25 @@ describe('Manual handler', () => {
137142
assert(r.message.match(/pokey/), 'string error should propagate');
138143
});
139144
it('Should allow explicit undefined', done => {
140-
const r = report('Turkey', undefined, undefined, () => {
141-
done();
142-
});
145+
const r = report(
146+
'Turkey', undefined as unknown as Request,
147+
undefined as unknown as string, () => {
148+
done();
149+
});
143150
assert(r.message.match(/Turkey/), 'string error should propagate');
144151
});
145152
it('Should allow request to be supplied as undefined', done => {
146-
const r = report('turnkey', undefined, 'solution', () => {
147-
done();
148-
});
153+
const r =
154+
report('turnkey', undefined as unknown as Request, 'solution', () => {
155+
done();
156+
});
149157
assert.strictEqual(r.message, 'solution', 'error should propagate');
150158
});
151159
it('Should allow additional message', done => {
152-
const r = report('Mickey', {method: 'SNIFF'}, undefined, () => {
153-
done();
154-
});
160+
const r = report(
161+
'Mickey', {method: 'SNIFF'}, undefined as unknown as string, () => {
162+
done();
163+
});
155164
assert(
156165
r.message.match(/Mickey/) && !r.message.match(/SNIFF/),
157166
'string error should propagate');

0 commit comments

Comments
 (0)