Add label printing functionality and error handling in stock and label flows
This commit is contained in:
@@ -52,3 +52,57 @@ export async function previewLabel(store, body) {
|
||||
|
||||
throw new Error('Label preview response did not include an image.');
|
||||
}
|
||||
|
||||
export async function printItemLabel(store, uuidB64) {
|
||||
return apiRequest(store, `${getPath('items')}/${uuidB64}/print-label`, {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
function flattenDetails(details) {
|
||||
if (!details) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (typeof details === 'string') {
|
||||
return details;
|
||||
}
|
||||
|
||||
if (Array.isArray(details)) {
|
||||
return details
|
||||
.map((entry) => (typeof entry === 'string' ? entry : JSON.stringify(entry)))
|
||||
.join(' | ');
|
||||
}
|
||||
|
||||
if (typeof details === 'object') {
|
||||
return Object.entries(details)
|
||||
.map(([key, value]) => `${key}: ${value}`)
|
||||
.join(' | ');
|
||||
}
|
||||
|
||||
return String(details);
|
||||
}
|
||||
|
||||
export function formatPrintErrorMessage(error) {
|
||||
const status = error?.status || error?.cause?.status;
|
||||
const payload = error?.payload || error?.cause?.payload || {};
|
||||
const code = String(payload?.code || '').toLowerCase();
|
||||
const detailsText = flattenDetails(payload?.details || error?.details || error?.cause?.details);
|
||||
|
||||
let message;
|
||||
if (code === 'printer_unavailable') {
|
||||
message = 'Printer is unavailable.';
|
||||
} else if (code === 'print_failed') {
|
||||
message = 'Label printing failed.';
|
||||
} else if (status === 503) {
|
||||
message = 'Printer service is unavailable.';
|
||||
} else if (status === 404) {
|
||||
message = 'Saved item could not be found for printing.';
|
||||
} else if (status === 400) {
|
||||
message = 'Print request was invalid.';
|
||||
} else {
|
||||
message = error?.message || 'Printing failed.';
|
||||
}
|
||||
|
||||
return detailsText ? `${message} (${detailsText})` : message;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user