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 { ConfigService } from './config.service';
import { HttpService } from '../core-services/http.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. * Provides the general document structure for PDF documents, such as page margins, header, footer and styles.
* Also provides general purpose open and download functions. * Also provides general purpose open and download functions.

View File

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

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 { MotionXlsxExportService } from 'app/site/motions/services/motion-xlsx-export.service';
import { LocalPermissionsService } from 'app/site/motions/services/local-permissions.service'; import { LocalPermissionsService } from 'app/site/motions/services/local-permissions.service';
import { StorageService } from 'app/core/core-services/storage.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 * Interface to describe possible values and changes for
@ -251,14 +252,22 @@ export class MotionListComponent extends ListViewBaseComponent<ViewMotion, Motio
if (result && result.format) { if (result && result.format) {
const data = this.isMultiSelect ? this.selectedRows : this.dataSource.filteredData; const data = this.isMultiSelect ? this.selectedRows : this.dataSource.filteredData;
if (result.format === 'pdf') { if (result.format === 'pdf') {
this.pdfExport.exportMotionCatalog( try {
data, this.pdfExport.exportMotionCatalog(
result.lnMode, data,
result.crMode, result.lnMode,
result.content, result.crMode,
result.metaInfo, result.content,
result.comments result.metaInfo,
); result.comments
);
} catch (err) {
if (err instanceof PdfError) {
this.raiseError(err.message);
} else {
throw err;
}
}
} else if (result.format === 'csv') { } else if (result.format === 'csv') {
this.motionCsvExport.exportMotionList(data, [...result.content, ...result.metaInfo]); this.motionCsvExport.exportMotionList(data, [...result.content, ...result.metaInfo]);
} else if (result.format === 'xlsx') { } 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 { MotionPdfService, InfoToExport } from './motion-pdf.service';
import { ConfigService } from 'app/core/ui-services/config.service'; import { ConfigService } from 'app/core/ui-services/config.service';
import { ViewCategory } from '../models/view-category'; import { ViewCategory } from '../models/view-category';
import { PdfError } from 'app/core/ui-services/pdf-document.service';
/** /**
* Service to export a list of motions. * Service to export a list of motions.
@ -64,22 +65,30 @@ export class MotionPdfCatalogService {
const motionDocList = []; const motionDocList = [];
for (let motionIndex = 0; motionIndex < motions.length; ++motionIndex) { for (let motionIndex = 0; motionIndex < motions.length; ++motionIndex) {
const motionDocDef: any = this.motionPdfService.motionToDocDef( try {
motions[motionIndex], const motionDocDef: any = this.motionPdfService.motionToDocDef(
lnMode, motions[motionIndex],
crMode, lnMode,
contentToExport, crMode,
infoToExport, contentToExport,
commentsToExport infoToExport,
); commentsToExport
);
// add id field to the first page of a motion to make it findable over TOC // add id field to the first page of a motion to make it findable over TOC
motionDocDef[0].id = `${motions[motionIndex].id}`; motionDocDef[0].id = `${motions[motionIndex].id}`;
motionDocList.push(motionDocDef); motionDocList.push(motionDocDef);
if (motionIndex < motions.length - 1) { if (motionIndex < motions.length - 1) {
motionDocList.push(this.pageBreak); 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);
} }
} }

View File

@ -90,6 +90,12 @@ export class MotionPdfService {
): object { ): object {
let motionPdfContent = []; 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 // determine the default lnMode if not explicitly given
if (!lnMode) { if (!lnMode) {
lnMode = this.configService.instant('motions_default_line_numbering'); lnMode = this.configService.instant('motions_default_line_numbering');