java.time包的一次踩坑,报错仅 clock/month/temporal 等文案问题。

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-28 10:51:59

事情起因是线上接口出现报错

 {
  "code" : 1,
  "msg" : "temporal",
  "data" : null
}

当时就懵逼过去。我们一般的异常都有断言去处理。这个报错意味着

e.getMessage() 仅仅是个 temporal

再修改报错返回发布之后获得了完整报错信息。

LocalDate.from(temporal);
// LocalDate 源码
public static LocalDate from(TemporalAccessor temporal) {
    Objects.requireNonNull(temporal, "temporal");
    LocalDate date = temporal.query(TemporalQueries.localDate());
    if (date == null) {
        throw new DateTimeException("Unable to obtain LocalDate from TemporalAccessor: " +
                temporal + " of type " + temporal.getClass().getName());
    }
    return date;
}

这报错倒是简单了。但是碰到的人就懵逼了。java.time 包的都是这种写法。

对比下其他类的源码

// StringJoiner 源码
public StringJoiner(CharSequence delimiter,
                    CharSequence prefix,
                    CharSequence suffix) {
    Objects.requireNonNull(prefix, "The prefix must not be null");
    Objects.requireNonNull(delimiter, "The delimiter must not be null");
    Objects.requireNonNull(suffix, "The suffix must not be null");
    // make defensive copies of arguments
    this.prefix = prefix.toString();
    this.delimiter = delimiter.toString();
    this.suffix = suffix.toString();
    this.emptyValue = this.prefix + this.suffix;
}

坑呀

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!