import { QBError } from 'quickblox/quickblox';
export const jsonParse =
(text: string): P | string => {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return JSON.parse
(text);
} catch (error) {
return text;
}
};
export const parseErrorObject = (data: Dictionary) =>
Object.keys(data)
.map((key) => {
const field = data[key];
return Array.isArray(field)
? `${key} ${field.join('')}`
: `${key} ${field}`;
})
.join(' ')
.replace(/errors\s?/, '');
export const parseErrorMessage = (message: string) => {
const data = jsonParse>(
message,
);
if (typeof data === 'string') {
return data;
}
if (Array.isArray(data)) {
return data.join('');
}
if (typeof data === 'object') {
return parseErrorObject(data);
}
return data;
};
export function isQBError(error: unknown): error is QBError {
return typeof error === 'object' && error !== null && 'message' in error;
}
export function stringifyError(error: unknown): string {
if (typeof error === 'string') return error;
if (error && typeof error === 'object') {
const dataError:
| { detail?: string; message?: string }
| Dictionary = error;
if (dataError.detail) {
return parseErrorMessage(dataError.detail);
}
if (dataError?.message) {
return parseErrorMessage(dataError.message);
}
return parseErrorObject(dataError);
}
return JSON.stringify(error);
}
export const isSessionDoesNotExistError = (error: string) => {
return (
error.toUpperCase().replaceAll(' ', '_').replace(/[.:]/g, '') ===
'BASE_REQUIRED_SESSION_DOES_NOT_EXIST'
);
};