Add label printing functionality and error handling in stock and label flows
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful

This commit is contained in:
2026-04-10 22:08:01 +02:00
parent 1dc1bb4912
commit e1383c4d56
10 changed files with 627 additions and 21 deletions
+54
View File
@@ -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;
}