Skip to content

Commit

Permalink
Update message logging to include more details (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterMuriuki authored Nov 6, 2024
1 parent e95461e commit 22c0ff9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ it('error when fetching the registration form', async () => {
[
{
"level": "error",
"message": "Operation to fetch form: 3623, failed with err: Request failed for | URL: https://test-api.ona.io/api/v1/forms/3623 | Status: 400",
"message": "Operation to fetch form: 3623, failed with err: Error Name: Error | Message: Request failed for | URL: https://test-api.ona.io/api/v1/forms/3623 | Status: 400",
},
],
]);
Expand Down
28 changes: 16 additions & 12 deletions packages/core/src/services/onaApi/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const customFetch = async (input: RequestInfo, init?: RequestInit, logger

const response = await persistentFetch(input, requestOptionsWithRetry)
if (response && !response.ok) {
throw throwHttpError(response)
throwHttpError(response)
}
return await response.json()
};
Expand Down Expand Up @@ -102,10 +102,11 @@ export class OnaApiService {
return Result.ok<Form>(res);
})
.catch((err: Error) => {
const failResult = Result.fail<Form>(err, NETWORK_ERROR)
this.logger?.(
createErrorLog(`Operation to fetch form: ${formId}, failed with err: ${err}`)
createErrorLog(`Operation to fetch form: ${formId}, failed with err: ${failResult.error}`)
);
return Result.fail<Form>(err, NETWORK_ERROR);
return failResult
});
}

Expand Down Expand Up @@ -183,16 +184,18 @@ export class OnaApiService {
return Result.ok(res);
})
.catch((err: Error) => {
this.logger?.(
createErrorLog(
`Unable to fetch submissions for form id: ${formId} page: ${paginatedSubmissionsUrl} with err : ${err.message}`
)
);

let recsAffected = pageSize;
if ((totalSubmissions - (page * pageSize)) < pageSize) [
recsAffected = totalSubmissions - (page * pageSize)
]
return Result.fail<FormSubmissionT[]>(err, { code: NETWORK_ERROR, recsAffected, });
const failResult = Result.fail<FormSubmissionT[]>(err, { code: NETWORK_ERROR, recsAffected, });
this.logger?.(
createErrorLog(
`Unable to fetch submissions for form id: ${formId} page: ${paginatedSubmissionsUrl} with err : ${failResult.error}`
)
);
return failResult
});
} while (page * pageSize <= totalSubmissions);
}
Expand Down Expand Up @@ -239,12 +242,13 @@ export class OnaApiService {
return Result.ok<Record<string, string>>(res);
})
.catch((err) => {
const failResult = Result.fail(err, NETWORK_ERROR)
this.logger?.(
createErrorLog(
`Failed to edit sumbission with _id: ${submissionPayload._id} for form with id: ${formId} with err: ${err.message}`
`Failed to edit sumbission with _id: ${submissionPayload._id} for form with id: ${formId} with err: ${failResult.error}`
)
);
return Result.fail(err, NETWORK_ERROR);
return failResult;
});
}
}
Expand Down Expand Up @@ -283,5 +287,5 @@ function throwHttpError(response: Response) {
? errorDetails.join(' | ')
: "An unknown network error occurred.";

return errorMessage
throw new Error(errorMessage)
}

0 comments on commit 22c0ff9

Please sign in to comment.