4

I was wondering if there is a method in which I can guarantee that a certain JS code will always run no matter of any JS error on the site.

I want to make a logging script which logs the activity of my user in the admin page, so if something goes wrong I can easily recreate the workflow that caused an error.

So is there any method for this?

Thank you very much.

Okonai
  • 63
  • 8

1 Answers1

0

With any language or any program you cannot guarantee.

The only way to have error free program is to create one by testing thoroughly.

  1. Create unit test cases for all possibilities in respect to your user stories.
  2. Use of try...catch mdn link

  3. Console.log

As far as I've worked in JavaScript these are the ways to ensure your program runs, with browser this seems tricky at times as everything is at user end and you can only control is by doing robust coding.

Edit : By this comment I do not intend to convey that testing is the only way to achieve error free code, you should be knowing that but still follow the best practices of OOP. This is all as per my knowledge.

anshulix
  • 197
  • 11
  • 1
    "The only way to have error free program is to create one by testing thoroughly" so Donald Knuth was wrong? – symcbean Dec 22 '15 at 13:01
  • I could be wrong as well, but this is something I've learnt while coding in javaScript these years. Anyway if you have something to correct me I'd love to learn :) – anshulix Dec 22 '15 at 13:18
  • Induction and exhaustion are (IME) the most common proofs applied to code. I'm not saying that testing is not required nor that testing might be more effecient than a formal proof. Indeed, formal proofs cannot always be applied to object-oriented code. But a blanket statement that "testing thouroughly" is the only wany to have an error free program is plain wrong. There should also be other tools in your kit to reduce error injection - but I'm not here to teach CompSci. – symcbean Dec 22 '15 at 13:41
  • 1
    My statement has been taken in a wrong way. I understand the principles of coding and do understand how to write and enforce good coding techniques. My intention was that the last stop for any program would be testing, which I believe is true. Rest thanks for the thoughtful comment and sharing. – anshulix Dec 22 '15 at 13:49
  • My goal is to create a script which logs any user activity till the error occurs, so I can debug more easily, and test the fix with the exact same action flow. – Okonai Dec 22 '15 at 14:51
  • @Okonai Well yes you could do that, there are many tools providing this. Possible answer for this http://stackoverflow.com/questions/18713415/user-activity-tracking-or-logging-with-javascript – anshulix Dec 22 '15 at 14:56
  • It is quite simple you need to create a script which captures every event occurring on what elements in your DOM. – anshulix Dec 22 '15 at 14:57
  • Yes, but if a syntax error occurs or other JS script stops working, how can I assure that my logging script is still up and running? – Okonai Dec 22 '15 at 15:16
  • This is simple It would stop working at that stop. At exact that event which would in-turn give you the point where your code stopped. And also syntax error would mean that your code will not even run at the first place which should be checked in testing phase, your console shows where error occurred. – anshulix Dec 22 '15 at 15:21