[api] return exception in json format, pretty pring in debug, reduce cod...

...e dups.