diff --git a/src/parse.js b/src/parse.js index 52c019c..85ad1f3 100644 --- a/src/parse.js +++ b/src/parse.js @@ -484,6 +484,11 @@ TimestampParser.parseFormatString = function parseFormatString(format){ addCharacter(ch); } } + if(directive) throw new TimestampParseError( + "Found unterminated directive at the end of the format string.", { + format: formatString + } + ); if(tokens.length && tokens[tokens.length - 1].string === "Z"){ tokens.zuluTimezone = true; } diff --git a/test/canary-test.js b/test/canary-test.js index 4df340f..41220a4 100644 --- a/test/canary-test.js +++ b/test/canary-test.js @@ -973,6 +973,16 @@ function createTests(strtime){ message: `Failed with format "%Y-%m-%?": Unknown directive "%?".`, }); }); + this.test("unterminated directive produces a readable error", function(){ + assert.throws(() => strftime(new Date(), "%"), { + name: "TimestampParseError", + message: `Failed with format "%": Found unterminated directive at the end of the format string.`, + }); + assert.throws(() => strptime("2000-01-01", "%"), { + name: "TimestampParseError", + message: `Failed with format "%": Found unterminated directive at the end of the format string.`, + }); + }); this.test("parse format is an empty string", function(){ assert.throws(() => strptime("2000-01-01", ""), { name: "TimestampParseError",