Skip to content

Commit

Permalink
parse detail for notification body
Browse files Browse the repository at this point in the history
  • Loading branch information
mwangggg committed Jan 16, 2024
1 parent 361483a commit 2a3879e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/app/Shared/Services/Api.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class ApiService {

const headers = {};
headers['Content-Type'] = 'application/json';
return this.sendLegacyRequest('v2', 'rules', {
return this.sendLegacyRequest('v2', 'rules', 'Rule Upload Failed', {
method: 'POST',
body: JSON.stringify(rule),
headers: headers,
Expand Down Expand Up @@ -572,7 +572,7 @@ export class ApiService {

const body = new window.FormData();
body.append('template', file);
return this.sendLegacyRequest('v1', 'templates', {
return this.sendLegacyRequest('v1', 'templates', 'Template Upload Failed', {
body: body,
method: 'POST',
headers: {},
Expand Down Expand Up @@ -641,7 +641,7 @@ export class ApiService {

const body = new window.FormData();
body.append('probeTemplate', file);
return this.sendLegacyRequest('v2', `probes/${file.name}`, {
return this.sendLegacyRequest('v2', `probes/${file.name}`, 'Custom Probe Template Upload Failed', {
method: 'POST',
body: body,
headers: {},
Expand Down Expand Up @@ -839,7 +839,7 @@ export class ApiService {
body.append('recording', file);
body.append('labels', JSON.stringify(labels));

return this.sendLegacyRequest('v1', 'recordings', {
return this.sendLegacyRequest('v1', 'recordings', 'Recording Upload Failed', {
method: 'POST',
body: body,
headers: {},
Expand Down Expand Up @@ -873,7 +873,7 @@ export class ApiService {

const body = new window.FormData();
body.append('cert', file);
return this.sendLegacyRequest('v2', 'certificates', {
return this.sendLegacyRequest('v2', 'certificates', 'Certificate Upload Failed', {
method: 'POST',
body,
headers: {},
Expand Down Expand Up @@ -1365,6 +1365,7 @@ export class ApiService {
// Used for uploading. Prefer sendRequest for other operations
apiVersion: ApiVersion,
path: string,
title: string,
{ method = 'GET', body, headers = {}, listeners, abortSignal }: XMLHttpRequestConfig,
params?: URLSearchParams,
suppressNotifications = false,
Expand Down Expand Up @@ -1430,14 +1431,15 @@ export class ApiService {
if (skipStatusCheck) {
throw err;
}
return this.handleLegacyError<XMLHttpResponse>(err, req, suppressNotifications);
return this.handleLegacyError<XMLHttpResponse>(err, title, req, suppressNotifications);
}),
);
return req();
}

private handleLegacyError<T>(
error: Error,
title: string,
retry: () => Observable<T>,
suppressNotifications = false,
): ObservableInput<T> {
Expand All @@ -1450,7 +1452,12 @@ export class ApiService {
} else {
Promise.resolve(error.xmlHttpResponse.body as string).then((detail) => {
if (!suppressNotifications) {
this.notifications.danger(`Request failed (${error.xmlHttpResponse.status} ${error.message})`, detail);
try {
const body = JSON.parse(detail).data.reason;
this.notifications.danger(title, body);
} catch {
this.notifications.danger(title, detail);
}
}
});
}
Expand Down

0 comments on commit 2a3879e

Please sign in to comment.