try catch 没有得到 date-fns parseISO 错误

try catch doesn't get date-fns parseISO error

我似乎犯了一个简单的错误,我写了这段代码来获取格式化的日期,但是如果日期没有正确的字符串可以转换,class 应该会捕获错误,但它不起作用并且控制台中显示错误!

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

有什么想法吗?

当你传递错误的日期时,它会被捕获。在正确的位置但在缓存中,您在变量 this.parsedDate 中传递了字符串“error”,当您调用 gettter MMM_d_YYYY 时,getter 试图编译的是

//Value of this.parsedDate which is 'error' because of catch
return format('error', "MMM d,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, "MMM d,yyyy")
    }
 
}
 

const wrongDate='2020-12-20T04:18:21.471275';

console.log(new DateFormats(wrongDate).MMM_d_YYYY);

try..catch 只能作为最后的手段使用,因此只能在别无选择的情况下使用。

如果一个函数需要特定类型的参数,那么在调用它之前进行检查,不要只使用 try..catch 然后再处理错误。在这种情况下,date-fns parseISO 函数需要一个字符串来避免类型错误,因此请确保使用字符串或 return undefined 或类似的值。然后调用方可以检查响应并进行处理。

在这种情况下,如果catch块被执行,那么:

this.parsedDate = 'error';

被执行,所以当 MMM_d_YYYY 是 accessed/called 时,它会在字符串上调用 format一个 Date 对象,所以 date-fns 会抛出一个错误。

通过在 调用函数之前检查 来避免这两个错误,而不是在之后捕获错误。

如果你开始使用try..catch来处理不恰当的输入,你就迫使调用者也使用try..catch,所以它开始通过你的代码传播。通过处理不正确的输入(例如,通过简单地 return 无效日期),调用者可以使用 if 块检查 return 值并处理“错误" 这样,比 try..catch.

更经济

提供一个不传递参数的默认值也很好,在这种情况下,当前日期和时间似乎是合适的,所以:

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