Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User-provided error handler to catch errors in template commands #138

Closed
speedpro opened this issue Jul 7, 2020 · 6 comments
Closed

User-provided error handler to catch errors in template commands #138

speedpro opened this issue Jul 7, 2020 · 6 comments

Comments

@speedpro
Copy link
Contributor

speedpro commented Jul 7, 2020

Rather than preventing the document from being generated, I'd like to ignore "undefined" errors, and either 1) leave the original placeholder intact, OR 2) display a placeholder inside the document.

I know I can use javascript code, (e.g: case && case.owner && case.owner.name ? case.owner.name : 'YOUR NAME HERE'), but I need to make a system that "non-technical" people can use.
Ideally we would just say +++ case.owner.name +++, and it would ignore undefined errors and leave the original text as is.

I hope that makes sense.

Is this scenario possible, or should I be using something in the additionalJsContext to achieve this?

Also, I'm happy to create a pull request if you can point me in the right direction for setting up a dev environment.

Thanks!

@jjhbw
Copy link
Collaborator

jjhbw commented Jul 10, 2020

Thanks for your suggestion. Just to explore this idea further, what do you propose the API would look like? One thing that comes to mind is providing a callback to rejectNullish instead of a boolean...

I think this is behaviour that we want to push to userspace as much as possible to avoid cluttering the API.

Throwing an error when a field is undefined (rejectNullish) makes sense to me, and so does returning an empty string (rejectNullish == false).

Adding more flavors to handle undefined results is possible, but may end up complicating everything more than necessary.

@speedpro
Copy link
Contributor Author

Hey, sorry for the late response.

I agree with pushing to userspace. As I've thought about it, I think just being able to include a custom error handler would be the way to go. That way the developer has the flexibility to handle any use case that might come up.

I would imagine a callback function like the following:

function(
  error:ErrorObject , // the error object
  expression: String, // or Object. The actual expression (e.g. "case.subject.firstname" or "query(abc)") or an object with details of the expression, and the type of operation performed. In any case, it would be nice to have access to the original string so that we can put it back into the template if there's an error.
) {
  return content : String; //the content to display
  //OR
  throw error :  Error; //throw the error, and prevent template generation
} 

If no error handler is supplied, then it would keep the current functionality of halting template generation.

Now, I don't know whether the catch function should be async or sync, or whether it would easy to make it optional, but what do you think?

@jjhbw
Copy link
Collaborator

jjhbw commented Aug 5, 2020

Good idea!

I think we can extend the rejectNullish type to be boolean | ErrorHandler where ErrorHandler is the callback with a signature similar to the one you suggest.

This touches upon the code in processTemplate.ts (basically every occasion where a NullishCommandResultError is thrown. E.g. here:

throw new NullishCommandResultError(cmdRest);

I'll get to this at some point as i'm likely to need similar functionality in the future. Feel free to send a PR, though.

@jjhbw jjhbw changed the title Enhancement: Ignore 'undefined' error messages when generating documents User-provided error handler to catch errors in template commands Aug 18, 2020
@jjhbw
Copy link
Collaborator

jjhbw commented Aug 18, 2020

Thanks again for your PR. I played around with it a bit and tried a few cases. See the custom-errorhandler branch. Does this solve your use case?

@speedpro
Copy link
Contributor Author

Yep, I just tested it on your branch, and it works perfectly.

Thanks!

@jjhbw
Copy link
Collaborator

jjhbw commented Aug 19, 2020

See https://github.com/guigrpa/docx-templates/releases/tag/v4.4.0

Let me know if I missed something.

@jjhbw jjhbw closed this as completed Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants