Skip to content

feat: Add ErrorToHtmlConverter using templates - #765

Merged
jvherweg merged 1 commit into
mainfrom
feat/error-html
Jun 14, 2021
Merged

jvherweg merged 1 commit into
mainfrom
feat/error-html

Conversation

@jvherweg

@jvherweg jvherweg commented Jun 7, 2021

Copy link
Copy Markdown
Member

Closes #654.

Will also have to change after #721 is resolved.

@RubenVerborgh RubenVerborgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay!

}

public async handle({ representation }: RepresentationConverterArgs): Promise<Representation> {
const errors = await arrayifyStream(representation.data);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const errors = await arrayifyStream(representation.data);
// We expect the incoming stream to be an object stream with a single error
const errors = await arrayifyStream(representation.data);

Comment on lines +32 to +39
const options = {
name: error.name,
message: error.message,
stack: error.stack,
};

const template = await fsPromises.readFile(this.templatePath, 'utf8');
const html = this.engine.apply(template, options);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const options = {
name: error.name,
message: error.message,
stack: error.stack,
};
const template = await fsPromises.readFile(this.templatePath, 'utf8');
const html = this.engine.apply(template, options);
// Render the HTML template
const { name, message, stack } = error;
const variables = { name, message, stack };
const template = await fsPromises.readFile(this.templatePath, 'utf8');
const html = this.engine.apply(template, variables);

@jvherweg jvherweg Jun 14, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { name, message, stack } = error; doesn't work unfortunately, it doesn't catch the stack field due to how errors are constructed.

Edit: nvm, I was thinking of an issue when passing the error object directly as a parameter to the templating engine.

/**
* Converts an Error to an HTML presentation by filling in the provided template.
*/
export class ErrorToHtmlConverter extends TypedRepresentationConverter {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noted that there's nothing HTML-specific in this class, so the engine might be anything, really.
More of an observation; perhaps contentType could be a constructor param and we can do whatever.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I'll change it to ErrorToTemplateConverter

Comment thread src/util/ContentTypes.ts
export const APPLICATION_SPARQL_UPDATE = 'application/sparql-update';
export const APPLICATION_X_WWW_FORM_URLENCODED = 'application/x-www-form-urlencoded';
export const TEXT_HTML = 'text/html';
export const TEXT_TURTLE = 'text/turtle';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just changed the order so they're alphabetical

Comment thread templates/error/error.hbs
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ name }}</title>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<title>{{ name }}</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{{ name }}</title>

@jvherweg
jvherweg merged commit 9c0fa77 into main Jun 14, 2021
@jvherweg
jvherweg deleted the feat/error-html branch June 14, 2021 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create nice error writers

2 participants