Last Updated: February 25, 2016
·
1.143K
· bhousman

Jade Error Logging Like a Boss

Error logging is your friend.

When using Jade for templating your views, it's really helpful to validate your mixins (a.k.a. functions) with useful error messages to see where problems ultimately break down when they're being called. I've found this practice to be particularly helpful when you have a larger scale app and you're calling dozens or even hundreds of mixins.

The following is a simple error logger and an example implementation.

//--------------------------------------------------------
//- Error Logger
//- Argument: string (optional)
//- Example: +errorLogger('All arguments required.')
//--------------------------------------------------------

mixin errorLogger(error)
  if (!error)
    script.
      console.error('Error: Jade Mixin');
  else
    script.
      console.error('Error: ' + error);

//--------------------------------------------------------
//- Social Login
//- Argument: string, string, string, string
//- Example: +socialLogin('home', 'bounceInLeft', 'facebook', 'Connect')
//--------------------------------------------------------

mixin socialLogin(link, animation, icon, text)
  if (link && animation && icon && text)
    a(href="/#{link}", class="animated #{animation}")
      i(class="fa fa-#{icon}") 
      span= text
  else
    +errorLogger('All arguments required.')

//--------------------------------------------------------
//- Example Implementation
//--------------------------------------------------------

+socialLogin('home', 'bounceInLeft', 'facebook')
//- Whoops, I forgot to include the last argument, therefore it will fail and you'll see the following in your console:

Error: All arguments required.