Reputation: 4054
I'm writing a Gmail Addon with Appscript. If the application throws an unhandled error, it will be automatically logged on stackdriver and displayed in Stackdriver Error Reporting. However, I want to manually send errors to Stackdriver Error Reporting:
try{
thisMayThrowAnError();
catch(err){
console.error(new Error("Something Failed"))
}
In the example above, the error log is being displayed in Stackdriver Logs, but it won't appear on Stackdriver Error Reporting. Is there a way to achieve this without actually throwing it (and crashing the addon)?
Upvotes: 0
Views: 248
Reputation: 201378
If my understanding is correct, how about this workaround? In this workaround, it uses the method of projects.events.report
in Stackdriver Error Reporting API.
Before you use this sample script, please do the following operations.
https://www.googleapis.com/auth/cloud-platform
to the scopes using Manifests and others.try {
thisMayThrowAnError();
} catch(err) {
var projectId = "project-id-#####"; // Please set the project ID of project including your script.
var payload = { // Please modify this for your situation.
message: "Something Failed",
serviceContext: {service: "sample"},
context: {reportLocation: {functionName: "sampleFunctionName"}},
};
var url = "https://clouderrorreporting.googleapis.com/v1beta1/projects/" + projectId + "/events:report";
var params = {
method: "post",
payload: JSON.stringify(payload),
contentType: "application/json",
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
}
UrlFetchApp.fetch(url, params);
}
payload
is sample values. So please modify to your situation.From your question, I understood that you are using Google Apps Script. If you are using other language, please tell me. If this workaround was not the result you want, I apologize.
Upvotes: 2