OfficeExtension.Error class

如果处理请求时由于错误而拒绝承诺,则返回 context.sync()的错误对象。

属性

code

错误代码字符串,例如“InvalidArgument”。

debugInfo

调试信息 (用于详细记录错误,即通过 JSON.stringify(...)) 。

innerError

内部错误(如果适用)。

message

从 Office 应用程序传递的错误消息。

name

错误名称:“OfficeExtension.Error”。

stack

堆栈跟踪(如果适用)。

traceMessages

跟踪消息 (在调用 context.sync()之前通过context.trace()调用 添加的任何) 。 如果出现错误,则包含发生错误之前执行的所有跟踪消息。 这些消息有助于监视程序执行顺序并检测错误的情况。

属性详细信息

code

错误代码字符串,例如“InvalidArgument”。

code: string;

属性值

string

debugInfo

调试信息 (用于详细记录错误,即通过 JSON.stringify(...)) 。

debugInfo: DebugInfo;

属性值

innerError

内部错误(如果适用)。

innerError: Error;

属性值

message

从 Office 应用程序传递的错误消息。

message: string;

属性值

string

name

错误名称:“OfficeExtension.Error”。

name: string;

属性值

string

stack

堆栈跟踪(如果适用)。

stack: string;

属性值

string

traceMessages

跟踪消息 (在调用 context.sync()之前通过context.trace()调用 添加的任何) 。 如果出现错误,则包含发生错误之前执行的所有跟踪消息。 这些消息有助于监视程序执行顺序并检测错误的情况。

traceMessages: string[];

属性值

string[]

示例

// The following example shows how you can instrument a batch of commands
// to determine where an error occurred. The first batch successfully
// inserts the first two paragraphs into the document and cause no errors.
// The second batch successfully inserts the third and fourth paragraphs
// but fails in the call to insert the fifth paragraph. All other commands
// after the failed command in the batch are not executed, including the
// command that adds the fifth trace message. In this case, the error
// occurred after the fourth paragraph was inserted, and before adding the
// fifth trace message.

// Run a batch operation against the Word object model.
await Word.run(async (context) => {

    // Create a proxy object for the document body.
    const body = context.document.body;

    // Queue a command to insert the paragraph at the end of the document body.
    // Start a batch of commands.
    body.insertParagraph('1st paragraph', Word.InsertLocation.end);
    // Queue a command for instrumenting this part of the batch.
    context.trace('1st paragraph successful');

    body.insertParagraph('2nd paragraph', Word.InsertLocation.end);
    context.trace('2nd paragraph successful');

    // Synchronize the document state by executing the queued-up commands,
    // and return a promise to indicate task completion.
    await context.sync();

    // Queue a command to insert the paragraph at the end of the document body.
    // Start a new batch of commands.
    body.insertParagraph('3rd paragraph', Word.InsertLocation.end);
    context.trace('3rd paragraph successful');

    body.insertParagraph('4th paragraph', Word.InsertLocation.end);
    context.trace('4th paragraph successful');

    // This command will cause an error. The trace messages in the queue up to
    // this point will be available via Error.traceMessages.
    body.insertParagraph(0, '5th paragraph', Word.InsertLocation.end);
    // Queue a command for instrumenting this part of the batch.
    // This trace message will not be set on Error.traceMessages.
    context.trace('5th paragraph successful');
    await context.sync();
}).catch(function (error) {
    if (error instanceof OfficeExtension.Error) {
        console.log('Trace messages: ' + error.traceMessages);
    }
});

// Output: "Trace messages: 3rd paragraph successful,4th paragraph successful"