[Refactoring] Add exception handling code, add var for js variables