package com.tofly.common.oauth.compent;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.tofly.common.core.constant.CommonConstants;
import com.tofly.common.oauth.exception.ToflyAuth2Exception;

import java.io.IOException;

/**
 * @author Administrator
 * @date 2019/9/12  13:52
 * 异常格式化
 */
public class ToflyAuth2ExceptionSerializer extends StdSerializer<ToflyAuth2Exception> {

    public ToflyAuth2ExceptionSerializer() {
        super(ToflyAuth2Exception.class);
    }

    @Override
    public void serialize(ToflyAuth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeObjectField("code", CommonConstants.FAIL);
        jsonGenerator.writeStringField("message", e.getMessage());
        jsonGenerator.writeStringField("result", e.getErrorCode());
        jsonGenerator.writeEndObject();
    }
}
