package com.box.l10n.mojito.service.boxsdk;

import com.box.l10n.mojito.boxsdk.BoxSDKJWTProvider;
import com.box.l10n.mojito.boxsdk.BoxSDKServiceException;
import com.box.sdk.BoxAPIException;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import com.box.sdk.BoxUser;
import com.box.sdk.CreateUserParams;
import com.box.sdk.InMemoryLRUAccessTokenCache;
import com.box.sdk.JWTEncryptionPreferences;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * @author wyau
 */
@Component
public class BoxSDKAppUserService {

    private static final int UNLIMITED_SPACE = -1;
    private static final String MOJITO_APP_USER_NAME = "Mojito System User";

    /**
     * logger
     */
    static Logger logger = getLogger(BoxSDKAppUserService.class);

    @Autowired
    BoxSDKJWTProvider boxSDKJWTProvider;

    /**
     * Create a Box App User
     *
     * @return
     * @throws BoxSDKServiceException
     */
    public BoxUser.Info createAppUser(String clientId, String clientSecret, String publicKeyId,
                String privateKey, String privateKeyPassword, String enterpriseId) throws BoxSDKServiceException {

        logger.debug("Creating Box App User: {}", MOJITO_APP_USER_NAME);
        JWTEncryptionPreferences jwtEncryptionPreferences = boxSDKJWTProvider.getJWTEncryptionPreferences(publicKeyId, privateKey, privateKeyPassword);
        BoxDeveloperEditionAPIConnection appEnterpriseConnection = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(
                enterpriseId,
                clientId,
                clientSecret,
                jwtEncryptionPreferences,
                new InMemoryLRUAccessTokenCache(5));

        CreateUserParams createUserParams = new CreateUserParams();
        createUserParams.setSpaceAmount(UNLIMITED_SPACE);

        try {
            return BoxUser.createAppUser(appEnterpriseConnection, MOJITO_APP_USER_NAME, createUserParams);
        } catch (BoxAPIException e) {
            throw new BoxSDKServiceException("Couldn't create App User", e);
        }
    }
}

@@ -396,16 +396,55 @@
 rences;%0A
+import com.ibm.icu.text.MessageFormat;%0A
 import o
@@ -1290,24 +1290,42 @@
 xception %7B%0A%0A
+        try %7B%0A    
         logg
@@ -1393,16 +1393,20 @@
         
+   
+ 
 JWTEncry
@@ -1549,16 +1549,20 @@
         
+    
 BoxDevel
@@ -1689,16 +1689,20 @@
         
+  
+  
 enterpri
@@ -1703,24 +1703,28 @@
 terpriseId,%0A
+    
             
@@ -1749,24 +1749,28 @@
             
+    
 clientSecret
@@ -1771,16 +1771,20 @@
 Secret,%0A
+    
         
@@ -1833,16 +1833,20 @@
         
+    
 new InMe
@@ -1883,16 +1883,20 @@
         
+   
+ 
 CreateUs
@@ -1955,16 +1955,20 @@
         
+    
 createUs
@@ -2010,30 +2010,16 @@
 PACE);%0A%0A
-        try %7B%0A
         
@@ -2171,67 +2171,172 @@
     
-throw new BoxSDKServiceException(%22Couldn't create App User%22
+String msg = MessageFormat.format(%22Couldn't create App User: %7B0%7D%22, e.getResponse());%0A            logger.error(msg);%0A            throw new BoxSDKServiceException(msg
 , e)

Improve error logging/handling when create Box app user