Merge pull request #4637 from tsiegleauq/catch-pdf-creation-bugs

Show errors during PDF generation
This commit is contained in:
Emanuel Schütze 2019-04-26 21:41:46 +02:00 committed by GitHub
commit ea93c65c3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 27 deletions

View File

@ -8,6 +8,19 @@ import { TranslateService } from '@ngx-translate/core';
import { ConfigService } from './config.service';
import { HttpService } from '../core-services/http.service';
/**
* Custom PDF error class to handle errors in a safer way
*/
export class PdfError extends Error {
public __proto__: PdfError;
public constructor(public message: string) {
super(message);
const trueProto = new.target.prototype;
this.__proto__ = trueProto;
}
}
/**
* Provides the general document structure for PDF documents, such as page margins, header, footer and styles.
* Also provides general purpose open and download functions.

View File

@ -1315,13 +1315,8 @@ export class MotionDetailComponent extends BaseViewComponent implements OnInit,
* Click handler for the pdf button
*/
public onDownloadPdf(): void {
// TODO: apparently statue amendments never have line numbers and are always in crMode
if (this.motion.isStatuteAmendment()) {
this.pdfExport.exportSingleMotion(this.motion, LineNumberingMode.None, ChangeRecoMode.Diff);
} else {
this.pdfExport.exportSingleMotion(this.motion, this.lnMode, this.crMode);
}
}
/**
* Click handler for attachments

View File

@ -30,6 +30,7 @@ import { MotionMultiselectService } from 'app/site/motions/services/motion-multi
import { MotionXlsxExportService } from 'app/site/motions/services/motion-xlsx-export.service';
import { LocalPermissionsService } from 'app/site/motions/services/local-permissions.service';
import { StorageService } from 'app/core/core-services/storage.service';
import { PdfError } from 'app/core/ui-services/pdf-document.service';
/**
* Interface to describe possible values and changes for
@ -251,6 +252,7 @@ export class MotionListComponent extends ListViewBaseComponent<ViewMotion, Motio
if (result && result.format) {
const data = this.isMultiSelect ? this.selectedRows : this.dataSource.filteredData;
if (result.format === 'pdf') {
try {
this.pdfExport.exportMotionCatalog(
data,
result.lnMode,
@ -259,6 +261,13 @@ export class MotionListComponent extends ListViewBaseComponent<ViewMotion, Motio
result.metaInfo,
result.comments
);
} catch (err) {
if (err instanceof PdfError) {
this.raiseError(err.message);
} else {
throw err;
}
}
} else if (result.format === 'csv') {
this.motionCsvExport.exportMotionList(data, [...result.content, ...result.metaInfo]);
} else if (result.format === 'xlsx') {

View File

@ -6,6 +6,7 @@ import { ViewMotion, LineNumberingMode, ChangeRecoMode } from '../models/view-mo
import { MotionPdfService, InfoToExport } from './motion-pdf.service';
import { ConfigService } from 'app/core/ui-services/config.service';
import { ViewCategory } from '../models/view-category';
import { PdfError } from 'app/core/ui-services/pdf-document.service';
/**
* Service to export a list of motions.
@ -64,6 +65,7 @@ export class MotionPdfCatalogService {
const motionDocList = [];
for (let motionIndex = 0; motionIndex < motions.length; ++motionIndex) {
try {
const motionDocDef: any = this.motionPdfService.motionToDocDef(
motions[motionIndex],
lnMode,
@ -81,6 +83,13 @@ export class MotionPdfCatalogService {
if (motionIndex < motions.length - 1) {
motionDocList.push(this.pageBreak);
}
} catch (err) {
const errorText = `${this.translate.instant('Error during PDF creation of motion:')} ${
motions[motionIndex].identifierOrTitle
}`;
console.error(`${errorText}\nDebugInfo:\n`, err);
throw new PdfError(errorText);
}
}
// print extra data (title, preamble, categories, toc) only if there are more than 1 motion

View File

@ -90,6 +90,12 @@ export class MotionPdfService {
): object {
let motionPdfContent = [];
// Enforces that statutes should always have Diff Mode and no line numbers
if (motion.isStatuteAmendment()) {
lnMode = LineNumberingMode.None;
crMode = ChangeRecoMode.Diff;
}
// determine the default lnMode if not explicitly given
if (!lnMode) {
lnMode = this.configService.instant('motions_default_line_numbering');