Google App脚本自定义错误



我想在code.gs的google.script.run的函数中扔一个自定义错误,以便我可以在侧栏中显示足够的信息。到目前为止,我已经测试了以下代码,没有运气:

code.gs

function UserException(type, text) {
  this.type = type;
  this.text = text;
  //this.stack = (new Error()).stack;
}
UserException.prototype = Object.create(Error.prototype);
UserException.prototype.constructor = UserException;
function assignRangeToTechnician(technician)
{       
   if(technician!=null)
   {
    //some code 
   }else
    throw new UserException("Error","Technician was not selected");
}

sidebar.html

...
<script>
function btnSelectTech()
{
  google.script.run
   .withSuccessHandler(rangeSelected)
   .withFailureHandler(techniciansMessage)
   .assignRangeToTechnician(document.getElementById('selectTechnician').value);
}
function techniciansMessage(Message)
{     
  var outputMessage = document.getElementById('message');
  //here is where I log the Message value
  google.script.run.myLog("In techniciansMessage() - Message: " + Message);
  if (Message == null)
   outputMessage.innerHTML = "<p style='color:red;'>Error occured</p>";
  else
    if (Message.type == "Error")        
      outputMessage.innerHTML = "<p style='color:red;'>" + Message.text + "</p>";
    else if (Message.type == "Message")
      outputMessage.innerHTML = "<p style='color:#f3f3f3;'>" + Message.text + "</p>";
} 
</script>
...

当我运行代码时,请调用.withfailureHandler,但Message不保留正确的值。当我登录该消息时,我将读取"错误:"作为'消息'参数的内容。

您能帮忙吗?谢谢。

您可以使用此so线程提及。尝试将错误参数添加到您的功能。示例:

google.script.run.withFailureHandler(function (error) {
  showError(error, 'getMe'); 
}).getMe();

其他可能会有所帮助的其他参考

最新更新