エラー処理
Handle errors in your scripts using error codes and custom error-handling logic.
読み終わるまでの所要時間 2 分最終更新 23日前
スクリプト内でエラーを処理する方法を説明します。
エラーコード
Cloud Code は、スクリプトが例外をスローした場合に422エラーのラップ
エラーを処理するにはコードをtry/catchJavaScript
ラップされていない関数は、いずれかの API 呼び出しが失敗した場合にエラーを返します。一方、API 呼び出しをラップした場合は、エラーメッセージからログアウトし、エラーの詳細を取得できます。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
ログを確認して詳細を表示するには、Unity Cloud Dashboard を開き、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; }}
ロギング
loggerplayerIdenvironmentIdprojectIdJavaScript
カスタムログ属性を追加して、ログメッセージついてより多くのコンテキストを持つことができます。例えば、Economy の呼び出しが失敗した場合、logger.debug(message, ...logAttributes)logger.info(message, ...logAttributes)logger.warning(message, ...logAttributes)logger.error(message, ...logAttributes)logger.fatal(message, ...logAttributes)
currencyIdJavaScript
Unity Cloud Dashboard に移動し、Cloud Code > Logs (ログ) を選択して、ログの表示とフィルター処理を行います。 詳細については、ログの発行 を参照してください。logger.error("Failed to increment currency", {"error.message": err.message}, {"currencyId" : params.currencyId});
Unity Cloud Dashboard でのデバッグ
Unity Cloud Dashboard を使用してスクリプトをデバッグできます。 Cloud Code > Scripts (スクリプト) に移動し、デバッグするスクリプトを選択します。 Response (レスポンス) タブとスクリプトエディターは、エラーが発生した場所を示すのに役立ちます。 Response (レスポンス) タブには、エラーの行と列が表示されます。デバッガーは、この情報を使用し、注釈を追加して、エラーが発生した場所を Script Code (スクリプトコード) セクションで視覚的に示します。
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'); }}