Error handling
Handle errors in your scripts using error codes and custom error-handling logic.
Read time 2 minutesLast updated a day ago
Learn how to handle errors in your scripts.
Error codes
Cloud Code returns a422Error wrapping
Wrap your code intry/catchJavaScript
The non-wrapped function returns an error if any of the API calls fail. However, if you wrap your API calls, you can log out the error message and get more details about the error:const { CurrenciesApi } = require("@unity-services/economy-2.4");module.exports = async ({ params, context, logger }) => { const { projectId, playerId } = context; const { currencyId } = params; const currencies = new CurrenciesApi(context); const setBalance = await currencies.setPlayerCurrencyBalance({ projectId, playerId, currencyId, currencyBalanceRequest: { balance: 100 } }); const decrement = await currencies.decrementPlayerCurrencyBalance({ projectId, playerId, currencyId, currencyModifyBalanceRequest: { amount: 50 } }); const increment = await currencies.incrementPlayerCurrencyBalance({ projectId, playerId, currencyId, currencyModifyBalanceRequest: { amount: 10 } }); const result = await currencies.getPlayerCurrencies({ projectId, playerId }); return result.data;};
JavaScript
To check the logs and view the details, open the Unity Dashboard and select Products > Cloud Code > Logs.const { CurrenciesApi } = require("@unity-services/economy-2.4");module.exports = async ({ params, context, logger }) => { const { projectId, playerId } = context; const { currencyId } = params; const currencies = new CurrenciesApi(context); try { const setBalance = await currencies.setPlayerCurrencyBalance({ projectId, playerId, currencyId, currencyBalanceRequest: { balance: 100 } }); const decrement = await currencies.decrementPlayerCurrencyBalance({ projectId, playerId, currencyId, currencyModifyBalanceRequest: { amount: 50 } }); const increment = await currencies.incrementPlayerCurrencyBalance({ projectId, playerId, currencyId, currencyModifyBalanceRequest: { amount: 10 } }); const result = await currencies.getPlayerCurrencies({ projectId, playerId }); return result.data; } catch (err) { logger.error("Failed to increment currency", { "error.message": err.message }, { currencyId: currencyId }); throw err; }};
Logging
You can use theloggerplayerIdenvironmentIdprojectIdJavaScript
You can add custom log attributes to have more context about the log message. For example, you can add thelogger.debug(message, ...logAttributes);logger.info(message, ...logAttributes);logger.warning(message, ...logAttributes);logger.error(message, ...logAttributes);logger.fatal(message, ...logAttributes);
currencyIdJavaScript
Navigate to the Unity Dashboard and select Cloud Code > Logs to view and filter the logs. Refer to Emit logs for more information.logger.error("Failed to increment currency", { "error.message": err.message }, { currencyId: params.currencyId });
Debugging on the Unity Dashboard
You can debug your scripts through the Unity Dashboard. Navigate to Cloud Code > Scripts and select the script you want to debug. The Response tab and the script editor help indicate where errors have occurred. The Response tab shows the line and column of an error. The debugger uses this information and adds an annotation to visually indicate where the error occurred in the Script Code section.
An example of error annotation in the Response tab.
JavaScript
const { CurrenciesApi } = require("@unity-services/economy-2.4");module.exports = async ({ params, context, logger }) => { const { playerId, projectId } = context; const { currencyId } = params; const currencies = new CurrenciesApi(context); try { const setBalance = await currencies.setPlayerCurrencyBalance({ projectId, playerId, currencyId, currencyBalanceRequest: { balance: 100, }, }); } catch (err) { if (err.response) { logger.error("Failed to set currency", { "error.response": err.response }, { currencyId: currencyId }); } else { logger.error("Failed to set currency", { "error.message": err.message }, { currencyId: currencyId }); } throw Error("oops"); }};