summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrancesco Mazzoli <francesco@rabbitmq.com>2012-08-23 14:10:47 +0100
committerFrancesco Mazzoli <francesco@rabbitmq.com>2012-08-23 14:10:47 +0100
commitac77b5c2deff7e94232385d40ed8bda054839037 (patch)
treea0590d9cf5c3b8af0cd2c9037ab7e0445c8367b1
parentb6f3a9ae3212e9241cc877c236439bdfc19e625c (diff)
downloadrabbitmq-server-ac77b5c2deff7e94232385d40ed8bda054839037.tar.gz
wrappers around mochijson2 to catch exceptions
-rw-r--r--src/rabbit_misc.erl20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl
index 8f6a9bcf..69d96ad4 100644
--- a/src/rabbit_misc.erl
+++ b/src/rabbit_misc.erl
@@ -60,6 +60,7 @@
-export([multi_call/2]).
-export([os_cmd/1]).
-export([gb_sets_difference/2]).
+-export([json_encode/1, json_decode/1]).
%% Horrible macro to use in guards
-define(IS_BENIGN_EXIT(R),
@@ -217,6 +218,8 @@
([pid()], any()) -> {[{pid(), any()}], [{pid(), any()}]}).
-spec(os_cmd/1 :: (string()) -> string()).
-spec(gb_sets_difference/2 :: (gb_set(), gb_set()) -> gb_set()).
+-spec(json_encode/1 :: (any()) -> string() | {'error', any()}).
+-spec(json_decode/1 :: (string()) -> any() | 'error').
-endif.
@@ -934,3 +937,20 @@ os_cmd(Command) ->
gb_sets_difference(S1, S2) ->
gb_sets:fold(fun gb_sets:delete_any/2, S1, S2).
+
+json_encode(Term) ->
+ try
+ mochijson2:encode(Term)
+ catch
+ exit:{json_encode, E} ->
+ {error, E}
+ end.
+
+json_decode(Term) ->
+ try
+ mochijson2:decode(Term)
+ catch
+ %% Sadly `mochijson2:decode/1' does not offer a nice way to catch
+ %% decoding errors...
+ error:_ -> error
+ end.