mirror of
https://github.com/anikeen-com/print-utils.git
synced 2026-03-13 13:46:07 +00:00
Handle errors in print requests
This commit is contained in:
23
README.md
23
README.md
@@ -86,7 +86,7 @@ curl --location 'http://127.0.0.1:1903/printers/PM-241-BT%20(Network)/print' \
|
|||||||
--form 'copies="1"'
|
--form 'copies="1"'
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Response:
|
#### Successful Response:
|
||||||
|
|
||||||
> On windows the response does not return any real job id. Instead, it returns a 0.
|
> On windows the response does not return any real job id. Instead, it returns a 0.
|
||||||
|
|
||||||
@@ -96,4 +96,23 @@ curl --location 'http://127.0.0.1:1903/printers/PM-241-BT%20(Network)/print' \
|
|||||||
"1"
|
"1"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Error Responses:
|
||||||
|
|
||||||
|
If the request is not successful, the response will contain an error message and an error id.
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"error": "Missing PDF file",
|
||||||
|
"error_id": "missing_pdf"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Here is a list of possible error ids (but not all of them are implemented yet):
|
||||||
|
|
||||||
|
| Error ID | Description |
|
||||||
|
|---------------|-------------------------------|
|
||||||
|
| missing_pdf | Missing PDF file |
|
||||||
|
| invalid_label | Invalid label format |
|
||||||
|
| null | Unsuccessful print job: {...} |
|
||||||
45
index.js
45
index.js
@@ -19,15 +19,21 @@ app.get('/', (req, res) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
app.get('/printers', async (req, res) => {
|
app.get('/printers', async (req, res) => {
|
||||||
const printers = await getPrinters()
|
try {
|
||||||
return res.json(printers.map(printer => {
|
const printers = await getPrinters()
|
||||||
return {
|
|
||||||
default: false,
|
return res.json(printers.map(printer => {
|
||||||
format: 'PDF',
|
return {
|
||||||
id: printer.printer || printer.deviceId,
|
default: false,
|
||||||
name: printer.printer || printer.name,
|
format: 'PDF',
|
||||||
}
|
id: printer.printer || printer.deviceId,
|
||||||
}))
|
name: printer.printer || printer.name,
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error getting printers', e)
|
||||||
|
return res.json([])
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post('/printers/:printer/print', upload.single('file'), async (req, res) => {
|
app.post('/printers/:printer/print', upload.single('file'), async (req, res) => {
|
||||||
@@ -36,14 +42,21 @@ app.post('/printers/:printer/print', upload.single('file'), async (req, res) =>
|
|||||||
// query: user-id: string, docType: label
|
// query: user-id: string, docType: label
|
||||||
// form data: file: binary, copies: string, shipment-ids: string
|
// form data: file: binary, copies: string, shipment-ids: string
|
||||||
|
|
||||||
const fileToPrint = req.file.path
|
try {
|
||||||
const printJob = await print(fileToPrint, printer)
|
const fileToPrint = req.file.path
|
||||||
const jobId = getRequestId(printJob)
|
const printJob = await print(fileToPrint, printer)
|
||||||
|
const jobId = getRequestId(printJob)
|
||||||
return res.json({
|
|
||||||
jobs: [jobId],
|
|
||||||
})
|
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
jobs: [jobId],
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error printing', e)
|
||||||
|
return res.status(400).json({
|
||||||
|
error: 'Unsuccessful print job: ' + e.message,
|
||||||
|
error_id: null,
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user