Smartling / java-api-sdk

SDK for integrating with the Smartling API.
https://api-reference.smartling.com/
Apache License 2.0
3 stars 10 forks source link

File is required exception on uploading files using SDK #56

Open avietrov opened 3 years ago

avietrov commented 3 years ago

We are using smartling java sdk 0.18 to upload dynamically generated json files to smartling. We use FilesAPI for that like this:

final InputStream stream = new ByteArrayInputStream(JACKSON_OBJECT_MAPPER.writeValueAsString(myDTOWithStrings));
final ImportTranslationsPTO pto =
    ImportTranslationsPTO.builder()
              .file(stream)
              .fileUri(fileUri)
              .fileType(JSON.name())
              .translationState(TranslationState.PUBLISHED)
              .overwrite(false)
              .build();
...
final ImportTranslationsResponse response =
                filesApi.importTranslations(projectId, locale, pto);

Quite often we get the exception:

com.smartling.api.v2.client.exception.client.ValidationErrorException: http_status=400, requestId=daedf180-7ea3-4651-92ba-847c777f4d58, top errors: 'File is required'
    at com.smartling.api.v2.client.exception.DefaultRestApiExceptionMapper.toExceptionByStatus(DefaultRestApiExceptionMapper.java:89)
    at com.smartling.api.v2.client.exception.DefaultRestApiExceptionMapper.toException(DefaultRestApiExceptionMapper.java:44)
    at com.smartling.api.files.v2.exceptions.FilesApiExceptionMapper.toException(FilesApiExceptionMapper.java:44)
    at com.smartling.api.v2.client.exception.RestApiExceptionHandler.createRestApiException(RestApiExceptionHandler.java:31)
    at com.smartling.api.v2.client.ExceptionDecoratorInvocationHandler.invoke(ExceptionDecoratorInvocationHandler.java:34)
    at com.sun.proxy.$Proxy101.importTranslations(Unknown Source)
    [my service stack trace]

Caused by: javax.ws.rs.BadRequestException: HTTP 400 Bad Request
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:220)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:196)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:62)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:151)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
    at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
    at com.sun.proxy.$Proxy100.importTranslations(Unknown Source)
    at jdk.internal.reflect.GeneratedMethodAccessor349.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.smartling.api.v2.client.ExceptionDecoratorInvocationHandler.invoke(ExceptionDecoratorInvocationHandler.java:30)
    ... 32 common frames omitted

Executing the code again usually helps, but I'd like to avoid that error in the first place. What does this error actually mean and how can we avoid getting it?

dkBrazz commented 3 years ago

'File is required' is returned by the Files API when the file parameter is not provided or doesn't have content

Please ensure that JACKSON_OBJECT_MAPPER.writeValueAsString(myDTOWithStrings) returns any content and stream variable is not read between instantiation and calling filesApi.importTranslations()

dkBrazz commented 3 years ago

It might be helpful to log serialized requests made by SDK using logging filter, so we could know if SDK sends an empty file or an API interpret non-empty file as empty

avietrov commented 3 years ago

Thanks for your response, I'll add some logging and get back to you :)