Friday, June 28, 2019

Stupid errors that took me more than 1 hour to solve

Here is my follows up on the original post here: https://blog.juliobiason.net/thoughts/things-i-learnt-the-hard-way/#code-reviews-are-not-for-style

The list will keep increasing day by day but hopefully i wont repeat any of the items here.

Soft-Hyphen Characters


https://en.wikipedia.org/wiki/Soft_hyphen

There was a request from my client to remove Soft-Hyphen character from a JSON.
A JSON use to render a page on Android and iOS.
Client did not specify the devices or OS, And I did not get the issue at first on Android.
But I could see some red-dot in JSON, I did a replace


replace({ '­':'', '­':'', '­': '' }) 

Sadly, It was not enough,
Took me some hours to find a solution here: https://stackoverflow.com/questions/10148184/php-check-if-string-contains-soft-hypen-and-replace-it/56885146#56885146

Even my friend told me that i should convert the U+00AD into a character and use that character to replace

And yes!, if you're checking the solution on StackOverflow, the answer is


$str = str_replace('­', '', $str);


And here was the diff on Stash



Throw Error in Promise.

What did i do?
I was trying to throw some error inside a Promise, but I forgot to put catch in the end of the end. My web server was hanging for several minutes before it responded.

function doAsync (){
  return new Promise( (resolve, reason ) =>{
      // do something to make error happens.
      if(error){
       throw new Error("ABC")
      }
     resolve("ok");
  });
}

For the code, we have two solutions. 1.1 Use resolve with the error.

function doAsync (){
  return new Promise( (resolve, reason ) =>{
      // do something to make error happens.
      if(error){
       return reason( new Error("ABC") );
      }
     resolve("ok");
  });
}

1.2 Use catch to return the error value.

function doAsync (){
  return new Promise( (resolve, reason ) =>{
      // do something to make error happens.
      if(error){
       throw new Error("ABC");
      }
     resolve("ok");
  }).catch(err => errorHandling(err))
}