package com.atlassian.stash.internal.rest.exception;

import com.atlassian.bitbucket.rest.RestErrorMessage;
import com.atlassian.bitbucket.rest.RestErrors;
import com.atlassian.bitbucket.rest.util.ResponseFactory;
import com.atlassian.bitbucket.rest.util.RestUtils;
import org.codehaus.jackson.map.exc.UnrecognizedPropertyException;

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

class UnrecognizedPropertyExceptionMapper implements ExceptionMapper<UnrecognizedPropertyException> {

    @Override
    public Response toResponse(UnrecognizedPropertyException exception) {
        RestErrorMessage error = new RestErrorMessage(exception.getUnrecognizedPropertyName(), exception.getLocalizedMessage());
        return ResponseFactory.status(Response.Status.BAD_REQUEST)
                .entity(new RestErrors(error))
                .type(RestUtils.APPLICATION_JSON_UTF8)
                .build();
    }
}
