| const CC_API_BASE_URL = 'https://support.google.com/s/community/api/'; |
| 9: 'FAILED_PRECONDITION', |
| // Function to wrap calls to the Community Console API with intelligent error |
| method, data, authenticated, authuser = 0, |
| returnUnauthorizedStatus = false) { |
| authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser); |
| return fetch(CC_API_BASE_URL + method + authuserPart, { |
| 'content-type': 'text/plain; charset=utf-8', |
| 'body': JSON.stringify(data), |
| 'credentials': (authenticated ? 'include' : 'omit'), |
| if (res.status == 200 || res.status == 400) { |
| return res.json().then(data => ({ |
| 'Status code ' + res.status + ' was not expected when calling ' + |
| // If the canonicalCode is PERMISSION_DENIED: |
| if (returnUnauthorizedStatus && res.body?.[2] == 7) |
| ('Response status 400 for method ' + method + '. ' + |
| (apiErrors[res.body?.[2]] ?? res.body?.[2] ?? 'unknown'))); |
| if (returnUnauthorizedStatus) |