Skip to content

Commit c2783e8

Browse files
committed
Fix validation error messages in the browser
We converted the ParsingError and ValidationError classes to inherit from the built-in Error class in #9025. This works fine in ES6, so our unit tests were happy. However, transpiling it down to ES5 leads to an absence of the message property, essentially removing the text of parsing and validation errors.
1 parent fc5c0de commit c2783e8

File tree

6 files changed

+20
-28
lines changed

6 files changed

+20
-28
lines changed

src/source/video_source.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class VideoSource extends ImageSource {
119119
if (this.video) {
120120
const seekableRange = this.video.seekable;
121121
if (seconds < seekableRange.start(0) || seconds > seekableRange.end(0)) {
122-
this.fire(new ErrorEvent(new ValidationError(`Playback for this video can be set only between the ${seekableRange.start(0)} and ${seekableRange.end(0)}-second mark.`)));
122+
this.fire(new ErrorEvent(new ValidationError(`sources.${this.id}`, null, `Playback for this video can be set only between the ${seekableRange.start(0)} and ${seekableRange.end(0)}-second mark.`)));
123123
} else this.video.currentTime = seconds;
124124
}
125125
}

src/style-spec/error/parsing_error.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// @flow
22

3-
export default class ParsingError extends Error {
3+
// Note: Do not inherit from Error. It breaks when transpiling to ES5.
4+
5+
export default class ParsingError {
6+
message: string;
47
error: Error;
58
line: number;
69

710
constructor(error: Error) {
8-
super(error.message);
911
this.error = error;
12+
this.message = error.message;
1013
const match = error.message.match(/line (\d+)/);
1114
this.line = match ? parseInt(match[1], 10) : 0;
1215
}

src/style-spec/error/validation_error.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// @flow
22

3-
export default class ValidationError extends Error {
3+
// Note: Do not inherit from Error. It breaks when transpiling to ES5.
4+
5+
export default class ValidationError {
6+
message: string;
47
identifier: ?string;
58
line: ?number;
69

7-
constructor(key: string | null, value?: any, message?: string, identifier?: string) {
8-
super([key, message].filter(a => a).join(': '));
10+
constructor(key: ?string, value: ?{ __line__: number }, message: string, identifier: ?string) {
11+
this.message = (key ? `${key}: ` : '') + message;
912
if (identifier) this.identifier = identifier;
1013

1114
if (value !== null && value !== undefined && value.__line__) {

src/util/evented.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ export class Event {
3131
}
3232
}
3333

34+
interface ErrorLike {
35+
message: string;
36+
}
37+
3438
export class ErrorEvent extends Event {
35-
error: Error;
39+
error: ErrorLike;
3640

37-
constructor(error: Error, data: Object = {}) {
41+
constructor(error: ErrorLike, data: Object = {}) {
3842
super('error', extend({error}, data));
3943
}
4044
}

test/unit/style-spec/validate.test.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@ import validate from '../../../src/style-spec/validate_style';
66

77
const UPDATE = !!process.env.UPDATE;
88

9-
function sanitizeError(error) {
10-
const sanitized = {};
11-
sanitized.message = error.message;
12-
for (const key in error) {
13-
sanitized[key] = error[key];
14-
}
15-
return sanitized;
16-
}
17-
189
glob.sync(`${__dirname}/fixture/*.input.json`).forEach((file) => {
1910
test(path.basename(file), (t) => {
2011
const outputfile = file.replace('.input', '.output');
2112
const style = fs.readFileSync(file);
2213
const result = validate(style);
2314
if (UPDATE) fs.writeFileSync(outputfile, JSON.stringify(result, null, 2));
2415
const expect = JSON.parse(fs.readFileSync(outputfile));
25-
t.deepEqual(result.map(sanitizeError), expect);
16+
t.deepEqual(result, expect);
2617
t.end();
2718
});
2819
});

test/unit/style-spec/validate_mapbox_api_supported.test.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@ import validateMapboxApiSupported from '../../../src/style-spec/validate_mapbox_
66

77
const UPDATE = !!process.env.UPDATE;
88

9-
function sanitizeError(error) {
10-
const sanitized = {};
11-
sanitized.message = error.message;
12-
for (const key in error) {
13-
sanitized[key] = error[key];
14-
}
15-
return sanitized;
16-
}
17-
189
glob.sync(`${__dirname}/fixture/*.input.json`).forEach((file) => {
1910
test(path.basename(file), (t) => {
2011
const outputfile = file.replace('.input', '.output-api-supported');
2112
const style = fs.readFileSync(file);
2213
const result = validateMapboxApiSupported(style);
2314
if (UPDATE) fs.writeFileSync(outputfile, JSON.stringify(result, null, 2));
2415
const expect = JSON.parse(fs.readFileSync(outputfile));
25-
t.deepEqual(result.map(sanitizeError), expect);
16+
t.deepEqual(result, expect);
2617
t.end();
2718
});
2819
});

0 commit comments

Comments
 (0)