Skip to content

Use error codes for extensive error descriptions - #795

Merged
jvherweg merged 3 commits into
mainfrom
feat/error-codes
Jul 6, 2021
Merged

jvherweg merged 3 commits into
mainfrom
feat/error-codes

Conversation

@jvherweg

@jvherweg jvherweg commented Jul 5, 2021

Copy link
Copy Markdown
Member

Closes #673.

I went back and forth several times deciding what the best place would be to do the conversion from code to text. This is what I ended up with.

How it works now is pretty similar to how we handled errors after the last change. The new thing is that the ErrorToTemplateConverter now also takes the error code into account. It looks in the codeFolder for a file matching the exact error code and uses that as a template for the error description if it finds a match. The reason I choose for a folder of templates instead of 1 JSON config with all descriptions in it is because JSON is bad for visualizing newlines.

The input parameters for the ErrorToTemplateConverter have changed to generate markdown now instead of html. This way the error templates can be written in markdown which seems nicer than having to write them in html. Markdown also seems more reusable if we want to return the error text in a non-html format in the future such as JSON. Html is now generated through the new MarkdownToHtmlConverter. This causes us to lose the <title> field with the error name but that seems acceptable.

Several errors also provide a value for the cause field in the error constructor now. Nothing is done with this yet but it might be useful in the future.

@jvherweg jvherweg added this to the v1.0.0 milestone Jul 5, 2021

@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.

Looks great and the markdown is definitely the way to go.

Just think that the extra HTML step is overkill:

Html is now generated through the new MarkdownToHtmlConverter. This causes us to lose the <title> field with the error name but that seems acceptable.

You can just use HTML tags in your main Markdown file, and then you can also keep the title.

const message = `Error verifying WebID via Bearer access token: ${(error as Error).message}`;
this.logger.warn(message);
throw new BadRequestHttpError(message);
throw new BadRequestHttpError(message, { cause: error });

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.

Note: we could also just catch (cause: unknown).

Comment thread src/storage/RoutingResourceStore.ts Resolved
Comment thread templates/error/error.md Outdated
@@ -0,0 +1,13 @@
# {{{ name }}}

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.

So you can actually do <html> and whatever tags you want in markdown. If using kramdown (if that still is a thing), you can then even continue using markdown syntax inside of those elements.

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.

But then you end up with a html/markdown hybrid which seems less prettier when used in non-html formats. That might also be specific for that library? Since I did look into a way to set something like a title in markdown and there seemed to be no general solution for this. The error descriptions would then also have to contain the entire html file since the title field would need to be in the head part, unless there's a library that takes care of that. At that point it seems to me that we lose a lot of the advantage of having the descriptions only in markdown and we might as well just write them in html directly?

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.

html/markdown hybrid

HTML tags are subset of the markdown language, so it's technically markdown: https://daringfireball.net/projects/markdown/syntax#html

The title can just be the error code or so?

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.

Although I'm happy actually with the way things are now.

Idea for title: the markdown could contain a stray <title> tag that is hoisted out. (I do this on my site.)

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.

Idea for title: the markdown could contain a stray <title> tag that is hoisted out. (I do this on my site.)

Yea can do that. It just means that the MarkdownToHtmlConverter needs to have some more logic to do this. Technically this could confuse people who for some reason have a markdown file starting with that title tag and did not expect this behaviour but I'm guessing that's not a large group

Comment thread templates/error/error.md Resolved
Comment thread templates/error/error.md Outdated
{{{ message }}}
{{/if}}

{{#if stack}}

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.

Let's wrap that in a <pre>.

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.

The markdown to html conversion library takes care of that due to the code tags.

Comment thread templates/template.html
@@ -0,0 +1,14 @@
<!DOCTYPE html>

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 think we can do without this one, although I do like a consistent template for everything (including IDP etc. at a later stage). Shall we name it main.html or so?

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.

Main reason for it is to have the title and meta tags (like viewport). Could also be used in the future for linking a css file.

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.

Yeah I do like that bit!

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.

Yeah that's great indeed; we should strive to use it as a base for all later on.

@jvherweg
jvherweg force-pushed the feat/error-codes branch from ba44e5a to 6707137 Compare July 6, 2021 12:20
@jvherweg
jvherweg force-pushed the feat/error-codes branch from 6707137 to c5d1d29 Compare July 6, 2021 12:26
@jvherweg
jvherweg merged commit f2f967f into main Jul 6, 2021
@jvherweg
jvherweg deleted the feat/error-codes branch July 6, 2021 14:42
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.

Assign codes to errors

2 participants