All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kasinf.framework.rest.web.SearchableErrorAttributes Maven / Gradle / Ivy

The newest version!
package com.kasinf.framework.rest.web;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * @author lkhsh
 */
public class SearchableErrorAttributes extends DefaultErrorAttributes {

    @Override
    public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
        Map body = super.getErrorAttributes(webRequest, options);
        Integer code = MapUtil.getInt(body, "status");
        String msg = MapUtil.getStr(body, "message");
        if (code == HttpStatus.SERVICE_UNAVAILABLE.value()) {
            msg = "内部错误!";
        }
        if (StrUtil.isEmpty(msg)) {
            msg = MapUtil.getStr(body, "error");
        }
        body.remove("message");
        body.remove("error");
        body.remove("status");
        body.put("code", code);
        body.put("msg", msg);
        return body;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy