package com.nantian.sc.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.nantian.sc.cloud.common.core.constant.CommonConstants;
import com.nantian.sc.cloud.common.security.exception.ScAuth2Exception;
import lombok.SneakyThrows;

/**
 *
 * @date 2018/11/16
 * <p>
 * OAuth2 异常格式化
 */
public class ScAuth2ExceptionSerializer extends StdSerializer<ScAuth2Exception> {
	public ScAuth2ExceptionSerializer() {
		super(ScAuth2Exception.class);
	}

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