Skip to content

Commit

Permalink
add more logging of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Sep 11, 2024
1 parent 5d860c2 commit 0c428a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function fetchAll(requests: MatomoRequestParams[], options: ApiFetchOptio
const allRequests = Object.keys(allUrlsMappedToIndex).join(', ');
let message = options.runtimeLimitAbortMessage || 'This request is taking too long, aborting.';
message = `${message} (Requests being sent: ${allRequests}).`;
throwUnexpectedError(message);
throwUnexpectedError(new Error(message), 'api client');
return;
}
}
Expand Down Expand Up @@ -245,9 +245,9 @@ export function fetchAll(requests: MatomoRequestParams[], options: ApiFetchOptio

if (errorResponses.length === 1) {
const { method, params } = requests[errorResponses[0].index];
throwUnexpectedError(`API method ${method} failed with: "${errorResponses[0].message}". (params = ${JSON.stringify(params)})`);
throwUnexpectedError(new Error(`API method ${method} failed with: "${errorResponses[0].message}". (params = ${JSON.stringify(params)})`), 'api client');
} else if (errorResponses.length > 1) {
throwUnexpectedError(`${errorResponses.length} API methods failed.`);
throwUnexpectedError(new Error(`${errorResponses.length} API methods failed.`), 'api client');
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ export function getSchema(request: GoogleAppsScript.Data_Studio.Request<Connecto
const { reportMetadata, goals, siteCurrency } = getReportMetadataAndGoalsAndCurrency(request);
if (!reportMetadata) {
const reportParams = JSON.parse(request.configParams.report);
throwUnexpectedError(`The "${reportParams.apiModule}.${reportParams.apiAction}" report cannot be found in the Matomo's report metadata. (All params = ${request.configParams.report})`);
throwUnexpectedError(new Error(`The "${reportParams.apiModule}.${reportParams.apiAction}" report cannot be found in the Matomo's report metadata. (All params = ${request.configParams.report})`), 'getSchema()');
}

const fields = getFieldsFromReportMetadata(reportMetadata, goals, siteCurrency);
Expand Down Expand Up @@ -547,7 +547,7 @@ export function getData(request: GoogleAppsScript.Data_Studio.Request<ConnectorP
let reportData = getReportData(request, requestedFields);
if (reportData === null) {
const reportParams = JSON.parse(request.configParams.report);
throwUnexpectedError(`The "${reportParams.apiModule}.${reportParams.apiAction}" report cannot be found in the Matomo's report metadata.`);
throwUnexpectedError(new Error(`The "${reportParams.apiModule}.${reportParams.apiAction}" report cannot be found in the Matomo's report metadata.`), 'getData()');
}

// API methods that return DataTable\Simple instances are just one row, not an array of rows, so we wrap them
Expand Down
12 changes: 8 additions & 4 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ export function throwUserError(message: string) {
* where the user can get support. This should be called for errors that are unexpected and could
* point to issues in the connector, or for errors that are not easy for the user to resolve themselves.
*
* @param message
* @param error Error the error that occurred
* @param callerId
* @throws Error
*/
export function throwUnexpectedError(message: string) {
export function throwUnexpectedError(error: Error, callerId?: string) {
try {
const { message, stack } = error;

log(`Unexpected error${callerId ? ` in ${callerId}` : ''}: ${stack}`);

const time = (new Date()).toString();
const wholeMessage = `An error has occurred - if you need help, please reach out in the Forums here: ${FORUM_URL} or `
+ `contact us by email at [email protected] (in your message, please use Looker Studio in the subject, and copy paste the error message). `
Expand Down Expand Up @@ -77,7 +82,6 @@ export function callWithUserFriendlyErrorHandling<T>(callerId: string, fn: () =>
throw e;
}

log(`Unexpected error: ${e.stack || e.message || e}`);
throwUnexpectedError(`${callerId}: ${e.message || e}`);
throwUnexpectedError(e, callerId);
}
}

0 comments on commit 0c428a4

Please sign in to comment.