public class RetrofitConversionException extends ServerIOException {
    public RetrofitConversionException(Throwable ex) {
        super(ex);
    }
} public class ServerIOException extends ServerException {

    public ServerIOException() {
        super();
    }

    public ServerIOException(String message) {
        super(message);
    }

    public ServerIOException(Throwable e) {
        super(e);
    }
} public class BadGatewayException extends HttpCodeException {
    public BadGatewayException(int code, ApiError response) {
        super(code, response);
    }
} public class BadRequestException extends HttpCodeException {
    public BadRequestException(int code, ApiError response) {
        super(code, response);
    }
} public class ConflictException extends HttpCodeException {
    public ConflictException(int code, ApiError response) {
        super(code, response);
    }
} public class FileTooBigException extends HttpCodeException {
    public FileTooBigException(int code, ApiError response) {
        super(code, response);
    }
} public class ForbiddenException extends HttpCodeException {
    public ForbiddenException(int code, ApiError response) {
        super(code, response);
    }
} public class GoneException extends HttpCodeException {
    public GoneException(int code, ApiError response) {
        super(code, response);
    }
} public class HttpCodeException extends ServerIOException {

    protected final int code;
    protected final ApiError response;

    public HttpCodeException(int code, ApiError response) {
        super();
        this.code = code;
        this.response = response;
    }

    public HttpCodeException(int code) {
        this(code, null);
    }

    public int getCode() {
        return code;
    }

    public ApiError getResponse() {
        return response;
    }

    @Override
    public String toString() {
        return "HttpCodeException{" +
                "code=" + code +
                ", response=" + response +
                '}';
    }
} public class InsufficientStorageException extends HttpCodeException {
    public InsufficientStorageException(int code, ApiError response) {
        super(code, response);
    }
} public class InternalServerException extends HttpCodeException {
    public InternalServerException(int code, ApiError response) {
        super(code, response);
    }
} public class LockedException extends HttpCodeException {
    public LockedException(int code, ApiError response) {
        super(code, response);
    }
} public class MethodNotAllowedException extends HttpCodeException {
    public MethodNotAllowedException(int code, ApiError response) {
        super(code, response);
    }
} public class NotAcceptableException extends HttpCodeException {
    public NotAcceptableException(int code, ApiError response) {
        super(code, response);
    }
} public class NotFoundException extends HttpCodeException {
    public NotFoundException(int code) {
        super(code);
    }

    public NotFoundException(int code, ApiError response) {
        super(code, response);
    }
} public class NotImplementedException extends HttpCodeException {
    public NotImplementedException(int code, ApiError response) {
        super(code, response);
    }
} public class PreconditionFailedException extends HttpCodeException {
    public PreconditionFailedException(int code, ApiError response) {
        super(code, response);
    }
} public class ServiceUnavailableException extends HttpCodeException {
    public ServiceUnavailableException(int code, ApiError response) {
        super(code, response);
    }
} public class TooManyRequestsException extends HttpCodeException {
    public TooManyRequestsException(int code, ApiError response) {
        super(code, response);
    }
} public class UnauthorizedException extends HttpCodeException {
    public UnauthorizedException(int code, ApiError response) {
        super(code, response);
    }
} public class UnprocessableEntityException extends HttpCodeException {
    public UnprocessableEntityException(int code, ApiError response) {
        super(code, response);
    }
} public class UnsupportedMediaTypeException extends HttpCodeException {
    public UnsupportedMediaTypeException(int code, ApiError response) {
        super(code, response);
    }
} public class ApiError {

    @SerializedName("description")
    String description;

    @SerializedName("error")
    String error;

    public String getDescription() {
        return description;
    }

    public String getError() {
        return error;
    }

    @Override
    public String toString() {
        return "ApiError{" +
                "description='" + description + '\'' +
                ", error='" + error + '\'' +
                '}';
    }
}
import com.google.gson.Gson;
import com.yandex.disk.rest.exceptions.RetrofitConversionException;
import com.yandex.disk.rest.exceptions.ServerIOException;
import com.yandex.disk.rest.exceptions.http.BadGatewayException;
import com.yandex.disk.rest.exceptions.http.BadRequestException;
import com.yandex.disk.rest.exceptions.http.ConflictException;
import com.yandex.disk.rest.exceptions.http.FileTooBigException;
import com.yandex.disk.rest.exceptions.http.ForbiddenException;
import com.yandex.disk.rest.exceptions.http.GoneException;
import com.yandex.disk.rest.exceptions.http.HttpCodeException;
import com.yandex.disk.rest.exceptions.http.InsufficientStorageException;
import com.yandex.disk.rest.exceptions.http.InternalServerException;
import com.yandex.disk.rest.exceptions.http.LockedException;
import com.yandex.disk.rest.exceptions.http.MethodNotAllowedException;
import com.yandex.disk.rest.exceptions.http.NotAcceptableException;
import com.yandex.disk.rest.exceptions.http.NotFoundException;
import com.yandex.disk.rest.exceptions.http.NotImplementedException;
import com.yandex.disk.rest.exceptions.http.PreconditionFailedException;
import com.yandex.disk.rest.exceptions.http.ServiceUnavailableException;
import com.yandex.disk.rest.exceptions.http.TooManyRequestsException;
import com.yandex.disk.rest.exceptions.http.UnauthorizedException;
import com.yandex.disk.rest.exceptions.http.UnprocessableEntityException;
import com.yandex.disk.rest.exceptions.http.UnsupportedMediaTypeException;
import com.yandex.disk.rest.json.ApiError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit.ErrorHandler;
import retrofit.RetrofitError;
import retrofit.client.Response;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
/*
* (C) 2015 Yandex LLC (https://yandex.com/)
*
* The source code of Java SDK for Yandex.Disk REST API
* is available to use under terms of Apache License,
* Version 2.0. See the file LICENSE for the details.
*/

package com.yandex.disk.rest.retrofit;



public class ErrorHandlerImpl implements ErrorHandler {

    private static final Logger logger = LoggerFactory.getLogger(ErrorHandlerImpl.class);

    @Override
    public Throwable handleError(RetrofitError retrofitError) {
        RetrofitError.Kind kind = retrofitError.getKind();
        switch (kind) {
            case NETWORK:
                Throwable th = retrofitError.getCause();
                return th instanceof IOException ? th : new IOException(th);

            case CONVERSION:
                return new RetrofitConversionException(retrofitError.getCause());

            case HTTP:
                try {
                    Response response = retrofitError.getResponse();
                    int httpCode = response.getStatus();
                    return createHttpCodeException(httpCode, response.getBody().in());
                } catch (IOException ex) {
                    logger.debug("errorHandler", retrofitError);
                    return ex;
                }

            case UNEXPECTED:
                return new ServerIOException(retrofitError.getCause());

            default:
                return new ServerIOException("ErrorHandler: unhandled error " + kind.name());
        }
    }

    public static HttpCodeException createHttpCodeException(int httpCode, InputStream in) {
        return createHttpCodeException(httpCode, readApiError(in));
    }

    private static ApiError readApiError(InputStream in) {
        Reader reader = new InputStreamReader(in);
        return new Gson().fromJson(reader, ApiError.class);
    }

    private static HttpCodeException createHttpCodeException(int httpCode, ApiError apiError) {
        logger.debug("getStatus=" + httpCode);
        switch (httpCode) {
            case 400:
                return new BadRequestException(httpCode, apiError);
            case 401:
                return new UnauthorizedException(httpCode, apiError);
            case 403:
                return new ForbiddenException(httpCode, apiError);
            case 404:
                return new NotFoundException(httpCode, apiError);
            case 405:
                return new MethodNotAllowedException(httpCode, apiError);
            case 406:
                return new NotAcceptableException(httpCode, apiError);
            case 409:
                return new ConflictException(httpCode, apiError);
            case 410:
                return new GoneException(httpCode, apiError);
            case 412:
                return new PreconditionFailedException(httpCode, apiError);
            case 413:
                return new FileTooBigException(httpCode, apiError);
            case 415:
                return new UnsupportedMediaTypeException(httpCode, apiError);
            case 422:
                return new UnprocessableEntityException(httpCode, apiError);
            case 423:
                return new LockedException(httpCode, apiError);
            case 429:
                return new TooManyRequestsException(httpCode, apiError);
            case 500:
                return new InternalServerException(httpCode, apiError);
            case 501:
                return new NotImplementedException(httpCode, apiError);
            case 502:
                return new BadGatewayException(httpCode, apiError);
            case 503:
                return new ServiceUnavailableException(httpCode, apiError);