问题描述
我似乎犯了一个简单的错误,我写了这段代码来获取格式化的日期,但是如果日期没有正确的字符串可以转换,类应该捕获错误但它不起作用并且错误显示在控制台中!
import {format,parseISO} from 'date-fns';
class DateFormats {
// class methods
constructor(date) {
try{
this.parsedDate = parseISO(date);
}
catch(e){
this.parsedDate = new Date();
console.log('catch Error')
}
}
get MMM_d_YYYY() {
return format(this.parsedDate,"MMM d,yyyy")
}
}
const wrongDate='2020-*12-20T04:18:21.471275';
console.log(new DateFormats(wrongDate).MMM_d_YYYY);
控制台错误:
RangeError: Invalid time value
有什么想法吗?
解决方法
try..catch 只能作为最后的手段,所以只能在没有其他选择的情况下使用。
如果一个函数需要特定类型的参数,那么在调用它之前检查,不要只使用try..catch然后处理错误。在这种情况下,date-fns parseISO 函数需要一个字符串来避免类型错误,因此请确保使用字符串调用它,或者可能返回 undefined 或类似的值。然后调用者可以检查响应并处理它。
在这种情况下,如果执行了 catch 块,则:
this.parsedDate = 'error';
被执行,所以当 MMM_d_YYYY 被访问/调用时,它会在一个字符串上调用 format 当它需要一个 Date 对象时,所以 date-fns 会抛出一个错误。 >
通过检查调用函数之前来避免这两种错误,而不是在调用函数之后发现错误。
如果你开始使用 try..catch 来处理不适当的输入,你会强制调用者也使用 try..catch,所以它开始通过你的代码传播.通过处理不正确的输入(例如通过简单地返回一个无效日期),然后调用者可以使用 if 块检查返回值并以这种方式处理“错误”,这比 尝试..抓住。
提供一个不传递参数的默认值也很好,在这种情况下,当前日期和时间似乎是合适的,所以:
let format = require('date-fns/format')
let parseISO = require('date-fns/parseISO')
class DateFormats {
// class methods
// Default is current date and time
constructor(arg = new Date().toISOString()) {
// Ensure arg is a string and let date-fns deal with parsing it
// This might result in an invalid Date,but let the caller
// deal with that
this.parsedDate = parseISO(String(arg));
}
// Return timestamp in MMM d YYYY format or "Invalid Date"
get MMM_d_YYYY() {
// this.parsedDate might be an invalid Date,so check first
// as calling format on an invalid date throws an error
if (isNaN(this.parsedDate)) {
return this.parsedDate.toString(); // Invalid Date
}
// Otherwise,it's a valid Date so use it
return format(this.parsedDate,"MMM d,yyyy")
}
}
// Examples
[void 0,// default,no arg -> Jan 31,2021
'2020-12-20T04:18:21.471275',// valid timestamp -> Dec 20,2020
'fooBar' // invalid timestamp -> Invalid Date
].forEach(arg => console.log(arg + ' -> ' + (arg? new DateFormats(arg).MMM_d_YYYY : new DateFormats().MMM_d_YYYY)));
以上可以在npm.runkit运行
,当您传递错误的日期时,它会被赶上。在正确的位置,但在缓存中,您在变量 this.parsedDate 中传递字符串“error”,当您调用 gettter MMM_d_YYYY
时,getter 试图编译的是
//Value of this.parsedDate which is 'error' because of catch
return format('error',yyyy")
此函数抛出错误。因为 'error' 字符串无法格式化。
import {format,parseISO} from 'date-fns';
class DateFormats {
// class methods
constructor(date) {
try{
this.parsedDate= parseISO(date);
}
catch(e){
this.parsedDate=parseISO(new Date());
console.log(this.parsedDate)
}
}
get MMM_d_YYYY() {
return format(this.parsedDate,yyyy")
}
}
const wrongDate='2020-12-20T04:18:21.471275';
console.log(new DateFormats(wrongDate).MMM_d_YYYY);