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

com.alibaba.ocean.rawsdk.client.util.ExceptionParser Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.alibaba.ocean.rawsdk.client.util;

import java.io.IOException;
import java.util.Map;

import com.alibaba.ocean.rawsdk.client.exception.AuthServiceException;
import com.alibaba.ocean.rawsdk.client.exception.InvokeConnectException;
import com.alibaba.ocean.rawsdk.client.exception.InvokeTimeoutException;
import com.alibaba.ocean.rawsdk.client.exception.OceanException;
import com.alibaba.ocean.rawsdk.client.exception.SecurityException;
import com.alibaba.ocean.rawsdk.client.exception.UnsupportAPIException;
import com.alibaba.ocean.rawsdk.util.GenericsUtil;

/**
 * Build Exception for all kinds of
 * {@link com.alibaba.ocean.rawsdk.client.policy.Protocol}, base on the exption
 * string which got from Reader
 * 
 * @author xiaoning.qxn
 */
public class ExceptionParser {

	/**
	 * for Protocol.Json2
	 * @param exption
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Throwable buildException4Json2(Map exption) {
		Map m = exption;

		String errorCodeStr = (String) m.get("error_code");
		String errorMesage = (String) m.get("error_message");
		// if null ,then just return the result
		if (GenericsUtil.isBlank(errorMesage)) {
			errorMesage = "Unknow message defined in response.";
		}
		if (GenericsUtil.isBlank(errorCodeStr) || !GenericsUtil.isNumeric(errorCodeStr)) {
			return buildException(errorCodeStr, errorMesage);
		} else {
			int errorCode = Integer.parseInt(errorCodeStr);
			return buildException(errorCode, errorMesage);
		}
	}

	@SuppressWarnings("unchecked")
	public static Throwable buildException4OAuth2(Object exption) {
		Map m = (Map) exption;
		int errorCode = 401;
		String errorMesage = (String) m.get("error_description");
		return buildException(errorCode, errorMesage);
	}

	private static Throwable buildException(int errorCode, String errorMesage) {

		switch (errorCode) {
		case 400:
			return new SecurityException(errorMesage);
		case 401:
			return new AuthServiceException(errorMesage);
		case 404:
			return new UnsupportAPIException(errorMesage);
		case 502:
			return new InvokeConnectException(errorMesage);
		case 504:
			return new InvokeTimeoutException(errorMesage);
		}
		return new OceanException(String.valueOf(errorCode), errorMesage);
	}

	private static Throwable buildException(String errorCode, String errorMesage) {
		return new OceanException(errorCode, errorMesage);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy