既定のエラー メッセージをオーバーライドする

レポートで Power BI 埋め込み分析の既定のエラー メッセージを非表示にして、代わりにアプリの設計に合ったカスタム エラー メッセージを表示できます。

たとえば、次の既定のエラー ダイアログを置き換えることができます。

Power BI 埋め込み分析の既定のエラー ダイアログを示すスクリーンショット。

このカスタム エラー ダイアログでは、次の操作を行います。

カスタム エラー ダイアログを示すスクリーンショット。

エラーをオーバーライドする方法

カスタム エラー メッセージを使用するには、最初に既定の Power BI 埋め込み分析エラー メッセージを非表示にするには、hideErrors プロパティを Power BI 埋め込み分析構成オブジェクトで true するように設定します。 powerbi.embed(element, config) のこの構成には、その他の設定とオプションも含まれています。 詳細については、「レポート設定の構成」を参照してください。

let config = {
    type: 'report',
    tokenType: models.TokenType.Embed,
    accessToken: accessToken,
    embedUrl: embedUrl,
    id: embedReportId,
    permissions: permissions,
    settings: {
        hideErrors: true
    }
};

既定のエラー メッセージを非表示にすると、エラーが発生した場合にエラー ダイアログとメッセージが表示されなくなります。 エラーが発生したときにアプリのユーザーが一貫した有用な応答を得るには、エラー イベントを処理する必要があります。

エラーを処理するには、まず、error イベントをリッスンしてエラーを取得します。

report.off("error");
report.on("error", function(event) {
    // Handle errors
});

IError インターフェイスの level プロパティを使用すると、処理するエラーの種類を指定できます。

interface IError {
    message: string;
    detailedMessage?: string;
    errorCode?: string;
    level?: TraceType;
    technicalDetails?: ITechnicalDetails;
}

enum TraceType {
    Information = 0,
    Verbose = 1,
    Warning = 2,
    Error = 3,
    ExpectedError = 4,
    UnexpectedError = 5,
    Fatal = 6,
}

Fatal エラーは、レポートが応答しなくなるため、最も重大なエラーの種類です。 エンド ユーザーがエラー メッセージなしで応答しないレポートや破損したレポートに直面するのを防ぐために、必ず Fatal エラーを処理してください。

次のコード例は、error イベントをリッスンして処理することでエラーをオーバーライドする方法を示しています。 この例では、newAccessToken または error.detailedMessage 関数は表示されません。 示されている場合は、独自の関数を実装します。

// Embed the loadConfiguration that hides the default errors.
let config = {
    type: 'report',
    tokenType: models.TokenType.Embed,
    accessToken: accessToken,
    embedUrl: embedUrl,
    id: embedReportId,
    permissions: permissions,
    settings: {
        hideErrors: true
    }
};

// Get a reference to the embedded report HTML element.
let embedContainer = $('#embedContainer')[0];

// Embed the report and display it within the div container.
let report = powerbi.embed(embedContainer, config);

// Set report.off to remove any pre-existing error event handler.
report.off("error");

// Set report.on to add the new error event handler.
report.on("error", function(event) {
    const error = event.detail;

    // If the error level isn't Fatal, log the error and continue.
    if (error.level !== models.TraceType.Fatal) {
        console.error(error);
        return;
    }

    // If the Fatal error is TokenExpired, refresh the token.
    if (error.message === models.CommonErrorCode.TokenExpired) {
        // Implement your own function here.
        let newAccessToken = refreshToken();
        
        // Set the new access token.
        report.setAccessToken(newAccessToken);
    } else {
        // If the error isn't TokenExpired, show the custom
        // dialog with detailed error message in the iframe.
        // Implement your own function here.
        showError(error.detailedMessage);
    }
});