Error handling
Handle errors in your scripts using error codes and custom error-handling logic.
Read time 2 minutesLast updated 4 days 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 in the Unity Dashboard: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; }};
- In the Unity Dashboard, go to Development > Products.
- Select Cloud Code.
- Select Logs.
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
To view and filter the logs, do the following:logger.error("Failed to increment currency", { "error.message": err.message }, { currencyId: params.currencyId });
- In the Unity Dashboard, go to Development > Products.
- Select Cloud Code.
- Select Logs.
Debugging on the Unity Dashboard
You can debug your scripts through the Unity Dashboard. To select the script you want to debug, do the following:- In the Unity Dashboard, go to Development > Products.
- Select Cloud Code.
- Select Scripts.
- Select the script you want to debug.

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"); }};