package com.dottydingo.hyperion.service.marshall;

import com.dottydingo.hyperion.exception.BadRequestException;
import com.dottydingo.hyperion.exception.InternalException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.InputStream;
import java.io.OutputStream;

/**
 */
public class EndpointMarshaller
{
    private ObjectMapper objectMapper;

    public EndpointMarshaller()
    {
        try
        {
            objectMapper = new ObjectMapperBuilder().getObject();
        }
        catch (Exception ignore){}
    }

    public void setObjectMapper(ObjectMapper objectMapper)
    {
        this.objectMapper = objectMapper;
    }

    public <T> T unmarshall(InputStream inputStream, Class<T> type)
    {
        try
        {
            return objectMapper.readValue(inputStream,type);
        }
        catch (Exception e)
        {
            throw new BadRequestException("Error unmarshalling request.",e);
        }
    }


    public <T> void marshall(OutputStream outputStream, T value)
    {
        try
        {
            objectMapper.writeValue(outputStream,value);
        }
        catch(Exception e)
        {
            throw new InternalException("Error marhsalling response.",e);
        }

    }
}

@@ -894,24 +894,38 @@
 stException(
+String.format(
 %22Error unmar
@@ -940,18 +940,37 @@
  request
-.%22
+: %25s%22,e.getMessage())
 ,e);%0A   

Add more detail to unmarshalling errors