Skip to content
Open
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
14 changes: 13 additions & 1 deletion packages/flow-runtime/src/TypeContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,19 @@ export default class TypeContext {
}

warn <T, V: T | any> (type: Type<T>, input: V, prefix: string = '', path?: string[]): V {
const validation = this.validate(type, input, prefix, path);
let validation;
try {
validation = this.validate(type, input, prefix, path);
}
catch (error) {
if (error instanceof Error) {
this.emitWarningMessage(error.message);
}
else {
this.emitWarningMessage(String(error));
}
return input;
}
const message = makeWarningMessage(validation);
if (typeof message === 'string') {
this.emitWarningMessage(message);
Expand Down
26 changes: 26 additions & 0 deletions packages/flow-runtime/src/warn.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */
import {doesNotThrow, ok} from 'assert';

import t from './globalContext';

describe('Warn API', () => {
it('should emit a warning instead of throwing when validation internals throw', () => {
const shapeOfString = t.$shape(t.string());
const messages = [];

const emitWarningMessage = t.emitWarningMessage;
t.emitWarningMessage = (message: string) => {
messages.push(message);
};

try {
doesNotThrow(() => t.warn(shapeOfString, {name: 'Alice'}));
}
finally {
t.emitWarningMessage = emitWarningMessage;
}

ok(messages.length > 0);
ok(/Can only \$Shape<T> object types\./.test(messages[0]));
});
});