package com.lutao.cloud.common.security.component;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.lutao.cloud.common.core.constant.CommonConstants;
import com.lutao.cloud.common.security.exception.CloudAuth2Exception;
import lombok.SneakyThrows;

/**

 * @date 2018/11/16
 * <p>
 * OAuth2 异常格式化
 */
public class CloudAuth2ExceptionSerializer extends StdSerializer<CloudAuth2Exception>{

	public CloudAuth2ExceptionSerializer(){
		super(CloudAuth2Exception.class);
	}

	@Override
	@SneakyThrows
	public void serialize(CloudAuth2Exception value, JsonGenerator gen, SerializerProvider provider){
		gen.writeStartObject();
		gen.writeObjectField("code", CommonConstants.FAIL);
		gen.writeStringField("msg", value.getMessage());
		gen.writeStringField("data", value.getErrorCode());
		gen.writeEndObject();
	}

}
