Improve API error messages with known error codes
When an API call fails, sometimes a numeric code is returned indicating
a type of generic error. These errors are known, so this change returns
the message instead of the numeric code when it is known.
This is particularly useful for the "batch lock" feature, since this
changes the error message "Error code: 7" to "Error code:
PERMISSION_DENIED", which is much clearer to users.
Change-Id: I7eb98a0442f63ea5aa35886453df5ea6e524af99
diff --git a/src/common/api.js b/src/common/api.js
index b2075c3..29ecc3c 100644
--- a/src/common/api.js
+++ b/src/common/api.js
@@ -1,11 +1,31 @@
const CC_API_BASE_URL = 'https://support.google.com/s/community/api/';
+const apiErrors = {
+ 0: 'OK',
+ 1: 'CANCELLED',
+ 2: 'UNKNOWN',
+ 3: 'INVALID_ARGUMENT',
+ 4: 'DEADLINE_EXCEEDED',
+ 5: 'NOT_FOUND',
+ 6: 'ALREADY_EXISTS',
+ 7: 'PERMISSION_DENIED',
+ 8: 'RESOURCE_EXHAUSTED',
+ 9: 'FAILED_PRECONDITION',
+ 10: 'ABORTED',
+ 11: 'OUT_OF_RANGE',
+ 12: 'OUT_OF_RANGE',
+ 13: 'INTERNAL',
+ 14: 'UNAVAILABLE',
+ 15: 'DATA_LOSS',
+ 16: 'UNAUTHENTICATED',
+};
+
// Function to wrap calls to the Community Console API with intelligent error
// handling.
export function CCApi(
method, data, authenticated, authuser = 0,
returnUnauthorizedStatus = false) {
- var authuserPart =
+ let authuserPart =
authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser);
return fetch(CC_API_BASE_URL + method + authuserPart, {
@@ -40,7 +60,8 @@
throw new Error(
res.body[4] ||
('Response status 400 for method ' + method + '. ' +
- 'Error code: ' + (res.body[2] ?? 'unknown')));
+ 'Error code: ' +
+ (apiErrors[res.body?.[2]] ?? res.body?.[2] ?? 'unknown')));
}
if (returnUnauthorizedStatus)