Skip to content

Commit

Permalink
Merge pull request #1012 from geoadmin/pb-805-better-error-message-to…
Browse files Browse the repository at this point in the history
…o-big-print-spec

PB-805: Add error message for failed printing. - #minor
  • Loading branch information
ismailsunni authored Aug 6, 2024
2 parents 21f75cb + c7418ac commit 173c5ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/api/print.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const PRINTING_DEFAULT_POLL_INTERVAL = 2000 // interval between each polling of
const PRINTING_DEFAULT_POLL_TIMEOUT = 600000 // ms (10 minutes)

const SERVICE_PRINT_URL = `${API_SERVICES_BASE_URL}print3/print/mapviewer`
const MAX_PRINT_SPEC_SIZE = 1 * 1024 * 1024 // 1MB in bytes (should be in sync with the backend)

class GeoAdminCustomizer extends BaseCustomizer {
/** @param {string[]} layerIDsToExclude List of layer names to exclude from the print */
Expand Down Expand Up @@ -468,11 +469,18 @@ export async function createPrintJob(map, config) {
outputFilename,
dpi,
})
if (!isPrintingSpecSizeValid(printingSpec)) {
throw new PrintError('Printing spec is too large', 'print_request_too_large')
}
log.debug('Starting print for spec', printingSpec)
return await requestReport(SERVICE_PRINT_URL, printingSpec)
} catch (error) {
log.error('Error while creating print job', error)
throw new PrintError(`Error while creating print job: ${error}`)
if (error instanceof PrintError) {
throw error
} else {
throw new PrintError(`Error while creating print job: ${error}`)
}
}
}

Expand Down Expand Up @@ -504,3 +512,15 @@ export async function abortPrintJob(printJobReference) {
throw new PrintError('Could not abort print job')
}
}
/**
* Check if the size of the printing spec is not bigger than MAX_PRINT_SPEC_SIZE
*
* @param {Object} printingSpec
* @returns {boolean} True if not bigger than MAX_PRINT_SPEC_SIZE, false otherwise
*/
function isPrintingSpecSizeValid(printingSpec) {
const jsonString = JSON.stringify(printingSpec)
const byteLength = new TextEncoder().encode(jsonString).length

return byteLength <= MAX_PRINT_SPEC_SIZE
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export function usePrint(map) {
const currentJobReference = ref(null)
/** @type {PrintStatus} */
const printStatus = ref(PrintStatus.IDLE)
/** @type {PrintError} */
const printError = ref(null)

const store = useStore()

Expand Down Expand Up @@ -89,6 +91,7 @@ export function usePrint(map) {
log.error('Error while printing', error)
if (printStatus.value === PrintStatus.PRINTING) {
printStatus.value = PrintStatus.FINISHED_FAILED
printError.value = error
}
return null
} finally {
Expand All @@ -115,5 +118,6 @@ export function usePrint(map) {
print,
abortCurrentJob,
printStatus,
printError,
}
}
19 changes: 13 additions & 6 deletions src/modules/menu/components/print/MenuPrintSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { computed, inject, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { useStore } from 'vuex'
import { PrintError } from '@/api/print.api.js'
import {
PrintStatus,
usePrint,
Expand All @@ -21,7 +22,7 @@ const printGrid = ref(false)
const printLegend = ref(false)
const olMap = inject('olMap')
const { printStatus, print, abortCurrentJob } = usePrint(olMap)
const { printStatus, print, abortCurrentJob, printError } = usePrint(olMap)
const i18n = useI18n()
const store = useStore()
Expand Down Expand Up @@ -52,11 +53,17 @@ const selectedScale = computed({
},
})
const printErrorMessage = computed(() =>
printStatus.value === PrintStatus.FINISHED_ABORTED
? i18n.t('operation_aborted')
: i18n.t('operation_failed')
)
const printErrorMessage = computed(() => {
if (printStatus.FINISHED_ABORTED) {
return i18n.t('operation_aborted')
} else {
if (printError.value instanceof PrintError && printError.value.key) {
return i18n.t(printError.value.key)
} else {
return i18n.t('operation_failed')
}
}
})
watch(isSectionShown, () => {
store.dispatch('setPrintSectionShown', { show: isSectionShown.value, ...dispatcher })
Expand Down

0 comments on commit 173c5ca

Please sign in to comment.