gpt4 book ai didi

sharepoint - 沙盒 SharePoint 解决方案的错误处理策略

转载 作者:行者123 更新时间:2023-12-03 08:35:30 26 4
gpt4 key购买 nike

我希望为使用沙盒 Web 部件的 SharePoint 解决方案开发错误处理策略。我最初正在研究基于此 article 的通用异常处理方法。 ,但这不适用于沙盒 Web 部件。一旦在沙箱中引发了未处理的异常,用户代码服务就会获得控制权,因此不会达到基础 Web 部件中的异常处理。沙盒解决方案是否有任何既定的错误处理方法?

是否有人知道确定沙盒 Web 部件中何时引发未处理异常的方法,如果只是为了将显示的错误消息更改为更用户友好的消息?我想替换标准的“Web 部件错误:部分信任应用程序域中的沙盒代码包装器的 Execute 方法引发了未处理的异常:发生了意外错误。”至少消息。

谢谢,魔术师安迪。

最佳答案

实际上,您可以按照您提到的文章建议的方法进行操作。您只需为后代 Web 部件将要覆盖的所有虚拟属性和方法提供安全的可覆盖项。模式可以描述为:

  • 用可能引发异常的代码覆盖并密封每个应该被覆盖的虚拟属性和方法。
  • 使用相同的原型(prototype)创建可覆盖的虚拟副本,并在必要时从中调用基类。这应该被您的后代覆盖。
  • 在 try&catch 中从密封成员中调用新的可覆盖对象,如果在那里捕获异常,请记住该异常。
  • 渲染方法要么渲染通常的内容,要么渲染记住的错误消息。

  • 这是我使用的基类的躯干:
    public class ErrorSafeWebPart : WebPart {

    #region Error remembering and rendering

    public Exception Error { get; private set; }

    // Can be used to skip some code later that needs not
    // be performed if the web part renders just the error.
    public bool HasFailed { get { return Error != null; } }

    // Remembers just the first error; following errors are
    // usually a consequence of the first one.
    public void RememberError(Exception error) {
    if (Error != null)
    Error = error;
    }

    // You can do much better error rendering than this code...
    protected virtual void RenderError(HtmlTextWriter writer) {
    writer.WriteEncodedText(Error.ToString());
    }

    #endregion

    #region Overriddables guarded against unhandled exceptions

    // Descendant classes are supposed to override the new DoXxx
    // methods instead of the original overridables They should
    // not catch exceptions and leave it on this class.

    protected override sealed void CreateChildControls() {
    if (!HasFailed)
    try {
    DoCreateChildControls();
    } catch (Exception exception) {
    RememberError(exception);
    }
    }

    protected virtual void DoCreateChildControls()
    {}

    protected override sealed void OnInit(EventArgs e) {
    if (!HasFailed)
    try {
    DoOnInit(e);
    } catch (Exception exception) {
    RememberError(exception);
    }
    }

    protected virtual void DoOnInit(EventArgs e) {
    base.OnInit(e);
    }

    // Continue similarly with OnInit, OnLoad, OnPreRender, OnUnload
    // and/or others that are usually overridden and should be guarded.

    protected override sealed void RenderContents(HtmlTextWriter writer) {
    // Try to render the normal contents if there was no error.
    if (!HasFailed)
    try {
    DoRenderContents(writer);
    } catch (Exception exception) {
    RememberError(exception);
    }
    // If an error occurred in any phase render it now.
    if (HasFailed)
    RenderError(writer);
    }

    protected virtual void DoRenderContents(HtmlTextWriter writer) {
    base.RenderContents(writer);
    }

    #endregion
    }

    --- 费尔达

    关于sharepoint - 沙盒 SharePoint 解决方案的错误处理策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10295438/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com