A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

36 lines
1.8 KiB

import { sprintf } from "sprintf-js";
import { NotificationService } from "../service/NotificationService";
import { AlertifyNotificationService } from "../service/AlertifyNotificationService";
export function fetchHandler(response: any) {
const ns: NotificationService = new AlertifyNotificationService();
if (response.ok) {
return response.json().then((json: any) => {
// the status was ok and there is a json body
// return Promise.resolve({ json: json, response: response });
ns.success('Message sent succesfully' + sprintf(" (http code %d)", response.status));
}).catch((err: any) => {
// the status was ok but there is no json body
// return Promise.resolve({ response: response });
ns.success('Message sent succesfully' + sprintf(" (http code %d)", response.status));
});
} else {
return response.json().catch((err: any) => {
// the status was not ok and there is no json body
// throw new Error(response.statusText);
ns.error('Some error occured. Please try again.');
}).then((json: any) => {
// the status was not ok but there is a json body
// throw new Error(json.error.message); // example error message returned by a REST
// let delay = alertify.get('notifier', 'delay');
// alertify.set('notifier', 'delay', 30);
let errorMessage = "";
json.errors.forEach(function(data: any) {
errorMessage += sprintf("Field Name: %s \n Rejected value: %s \n Reason: %s \n", data.field_name, data.rejected_value, data.error_message);
});
ns.errorWithDelay(sprintf('There were errors in your message - %s', errorMessage), 30);
// alertify.set('notifier', 'delay', delay);
});
}
}