Three Ways to Handle Exceptions with Async Functions

Javascript exceptions shouldn’t be thrown when an IO  to error occurs. Instead, helpful messages should be sent your your logger system AND to the user.

Method 1: Try/Catch

Traditionally, these exceptions are handled using Try/Catch blocks, but those can easily make your code unreadable. For example,

Method 2: Catch Callback

Method 3: Custom Exception Wrapper

This method is inspired by Dima Grossman’s post.

Which to Choose?

Method 1 is just plain yuck. Method 2 is decent, I prefer Method 3 for readability.

What’s with Redux, ReduxSauce, Thunk, and Sagas?

What is Redux?

A library to create and manage data objects (aka state objects) based on the flux pattern.

Actions

Are requests to a store to take an action.

Example:
{ type: ADD_TODO, text: ‘Build my first Redux app’}

Action Types

An enum of allowable action types.

Reducers

Are handler functions for actions that apply the action payload.

myReducer = (state, action) => nextState

ReduxSauce

Is a library to make Redux more DRY. In doing so though, it makes the code less readable imo.

Thunk and Sagas

Calls to actions are synchronous. This means that action handlers cannot fetch data or have side effects. Thunk and Sagas solve this desire.

Reasons for: More powerful Redux can handle most of your biz logic
Reasons against: If not done carefully, you will end up with Redux spaghetti

Thunk

Thunk is an extended reducer which can (1) return a promise, and (2) actions are allowed to be called from within the promise (aka side-effects). For example, you can have an action which first sets a loading field = true in the store, fetches data, then updates the store when done.

Sagas

Sagas is like Thunk, but allows for action handling to be interrupted using generators

Further Reading

  • https://engineering.universe.com/what-is-redux-saga-c1252fc2f4d1

How to access object attributes based on a variable in Typescript

Typescript is obscurely particular with accessing attribute keys on objects that lack a generic signature. Adding generic signatures reduces type-safety though.

Here’s a Typescript-friendly way to verify an attribute exists in an object, and then access that attribute.

This post originally posted at https://blog.smartlogic.io/accessing-object-attributes-based-on-a-variable-in-typescript/

Drupal 7: Converting a text field to a long text (aka textarea) field

Realize later on that text field you created needs to actually be a long text field?  You’re not alone.

After doing some research, I found zero complete solutions.  The ones I found leave problemattic remnants in the database, which can cause hard to diagnose issues such as restricted field length.  See references.

Luckily for you, the drush script below will do a more complete conversion.

Thanks go out to the references though, for getting me started.

 

References:

  1. http://www.pixelite.co.nz/article/convert-existing-textfield-textarea-drupal-7/
  2. http://www.up2technology.com/blog/converting-drupal-fields/

 

Have FTP but no ssh? Web Shell to the rescue!

There are many times a web shell can come in handy, such as  running a non-shell OS or if you have FTP but non SSH.  Web shell to the rescue!  The top three I could find are b374k-shell, ajaxshell, and shellinabox.  b374k-shell is my favorite, because it is all-around better than ajaxshell, and it doesn’t require compilation like shellinabox.  So enter b374k-shell a php web app that emulates some core capabilities of a shell.  Here is my review.

Features:

  1. A web-app that is a self-contained php file
  2. Password protected – stores a hash of your password within the php file
  3. Secure if you have https configured
  4. basic shell
  5. file browser, with options to
    • upload
    • edit
    • download
    • create
    • delete
    • rename
    • hex view
    • compress
    • decompress
    • copy
    • paste
    • chmod
    • limited bulk operations
  6. process manager
  7. system info browser
  8. php/python/ruby/perl/gcc evaluator
  9. db shells

 

Limitations:

  1. Can’t sshfs
  2. Bulk upload/download
  3. Basic editor with no syntax highlighting
  4. Works on mobile, but isn’t optimized for mobile

 

Conclusion:  It isn’t as good as ssh, but it’s super cool and can do everything you need it to for web development.

Best options for SSHFS on Mac OSX

Thank heavens for SSHFS! SSHFS allows me to locally mount remote linux/unix filesystems. This allows me to create and edit remote files using all of my native software! Sadly, the documentation for SSHFS options are weak, and the default options never work well for me. I’ve used SSHFS nearly every day for the past 3 months, and here is my recommendation for options for OSX:

  • nohup – makes the connection more reliable
  • follow_symlinks – optional, but is necessary if you want symlink folders to work
  • allow_other – let’s you assume file priviledges of the remote user
  • no_readahead, noappledouble, nolocalcaches – reduces overhead and makes Finder work MUCH faster

BONUS Want to make SSHFS work beautifully with SSHFS?  Check out this teaser:

If you want this, follow these instructions:

  1. Create a mount folder
  2. In the folder, create a script with the extension “.command”
  3. Add this to the script:

  1. Allow the script to be executable.  I usually do this from the terminal by typing:

  1. Now when you double click the script, the folder will turn into your remote folder.  To go back, simple click the eject icon next to the folder.
  2. Profit.

Making Sketch Animation Gifs 101

Have you ever seen those commercials which feature sketch animation, like the Numbers Never Lie commercial during the Super Bowl?  You may have also heard it called video scribing, fast hand drawing, or sketch cartoon video.  Building on this style of animation, I wondered how hard it would be to do a similar effect on a tablet.  Even cooler, what if I made a sketch animation GIF to use as a loading page animated GIF?

To start, I needed an app that would allow me to draw and turn my strokes into a video.  I started by searching google for sketch animation tablet apps, but the results were just plain old animation apps.  No good.  Digging deeper, I remember that teachers make sketch animations all the time for online classes.  So instead of looking for teaching apps, and voila!  I found two apps:  ScreenChomp and Educreations.  They both appeared to offer what I needed:  video recording of a drawing, aka sketch animation.  Teacher’s have been using sketch animation for years!  Note:  I tried ScreenChomp first and it worked, so I didn’t try Educreations.

Now the next challenge: how to make a GIF from the sketch animation video.  There are web services out there that will do this for a fee, but 1) I wanted to learn how to do it myself and 2) I wanted a transparent background.  I heard you can use Movie Maker some how, but having a Mac I decided to use the iMovie method.

Here is my method:  I used this awesome tutorial to convert the video to gif using iMovie and GIFfun, then used GIMP to make the background transparent.  To make the background transparent, I went through every frame/layer of the GIF to 1) add an alpha channel, 2) Select the background using the color selector tool, then 3) Edit->Clear.  When you save, make sure you set the animation speed to ~20ms.  This is quite tedious, but you could automate it using a GIMP script if you were so inclined.

And that’s it!  Here is my animated sketch GIF with transparent background!

loading...

25

May

jQ.Mobi 1.0 vs. jQuery Mobile

A common complaint about jQuery Mobile is the size and speed. By giving up support of legacy browsers, frameworks like jQ.Mobi (aka jqmobi) and Zepto claim smaller footprints and superior performance.  According to most benchmarks I’ve seen like this one, jQ.Mobi delivers while Zepto falls short of it’s performance claims.  Also, jQ.Mobi claims support for IE9 and IE10.  For these reasons, I decided to give jQ.Mobi a shot.

To create a fair comparison, I recreated last post‘s jQuery Mobile demo using the jQ.Mobi+jQ.Ui framework.  Getting started with jQ.Ui is harder than it should be, especially since being acquired by Intel.  On the bright side though, it’s still open source and the developer is very friendly on GitHub.  The best way to start with jQ.Ui is to pull down the jQ.Mobi git repo and point your browser to the jQ.Ui kitchen sink.  From there, you can explore most of the features jQ.Ui has to offer.  I started this demo with the example project.

My overall impression with jQ.Ui is good and bad.  I confirmed every claim of performance and size.  My demo is footprint is tiny, very noticeably faster on both newer and older devices, and compatibility with mobile phones is superb.  I’d go so far to say that compatibility on phones is even better than jQuery Mobile.  On the other hand, jQuery Mobile is a lot easier to develop on because 1) The documentation is awesome, 2) the library of built in gadgets is much bigger, 3) customizing and theming is much more intuitive, 4) RIB will get you up and running wicked fast.

So for the time being, I’ll prototype with jQuery Mobile and move to jQ.Mobi if the extra work is warranted later.  Check out my jQ.Mobi demo here!

11

Mar

Initial thoughts with the jQuery Mobile Framework

I’ve been meaning to try the jQuery Mobile Framework for a while now, and after discussing with District Karaoke’s Jesse Rauch I felt inspired.

So far, I’m optimistic. The baked in look and feel make for a what appears to be a nice experience. However, JQM animations are a little ahead of the mobile support. The animations will not be smooth on most phones, and will sometimes not come from the correct direction. It seems entirely a performance and browser issue, and I think JQM will look much better in future phones.

So for now, JQM may not be the best choice for lower end phones. Stay tuned, because soon I’m going to report on how JQM compares to alternatives.  In the mean time, check out my demo and offer me your thoughts!

Quick tip: I found the easiest way to get up and running with jQuery Mobile is to use the awesome RIB tool.

28

Feb

How and when to use python’s lambda

There is a long running debate in python land of the usefulness of the lambda function, and I usually air on the side of avoidance.  However, for simple occasions it can make for cleaner code.

The python docs are essential for quick reference, but sometimes they skimp on obscure features.  Lambda is one of those features.

In a nutshell, lambda is the inline function creator for python. In other languages, inline functions often look like this:

myFunction is a function, and myFunction(1,2)=3.  That makes a lot of sense! myFunction can similarly be defined in python using lambda:

myFunction = lambda arg1,arg2:  arg1+arg2

It’s true that this syntax requires less typing, but at the cost of readability.  In case you were wondering, here is a good example of when to use it.

Without lambda:

With lambda:

Head caution though, because readability goes out the window whenever the lambda expression gets to complicated.

When not to use lambda: