package com.atlassian.bitbucket.internal.importer.rest;

import com.atlassian.bitbucket.internal.importer.UnsupportedScmForImportException;
import com.atlassian.bitbucket.rest.exception.UnhandledExceptionMapper;
import com.atlassian.bitbucket.rest.exception.UnhandledExceptionMapperHelper;
import com.atlassian.bitbucket.rest.util.ResponseFactory;
import com.sun.jersey.spi.resource.Singleton;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

import static javax.ws.rs.core.Response.Status.BAD_REQUEST;

@Provider
@Singleton
public class RepositoryImportExceptionMapper extends UnhandledExceptionMapper {

    public RepositoryImportExceptionMapper(UnhandledExceptionMapperHelper helper) {
        super(helper);
    }

    @Override
    public Response toResponse(Exception exception) {
        if (exception instanceof UnsupportedScmForImportException) {
            return ResponseFactory.error(BAD_REQUEST, "externalRepositories", exception.getLocalizedMessage()).build();
        }
        return super.toResponse(exception);
    }
}
