         request.put "messages/batchFetch"
         .send target
         .end (res) ->
-            if res.ok
-                callback null, res.body
-            else
-                if res.body?.name?
-                    res.error = res.body
-                callback res.error
+            handleResponse res, callback, "batchFetch"
 
     batchAddFlag: (target, flag, callback) ->
         body = _.extend {flag}, target
         request.put "messages/batchAddFlag"
         .send body
         .end (res) ->
-            if res.ok
-                callback null, res.body
-            else
-                if res.body?.name?
-                    res.error = res.body
-                callback res.error
+            handleResponse res, callback, "batchAddFlag"
 
     batchRemoveFlag: (target, flag, callback) ->
         body = _.extend {flag}, target
         request.put "messages/batchRemoveFlag"
         .send body
         .end (res) ->
-            if res.ok
-                callback null, res.body
-            else
-                if res.body?.name?
-                    res.error = res.body
-                callback res.error
+            handleResponse res, callback, "batchRemoveFlag"
 
     batchDelete: (target, callback) ->
         body = _.extend {}, target
         request.put "messages/batchTrash"
         .send target
         .end (res) ->
-            if res.ok
-                callback null, res.body
-            else
-                if res.body?.name?
-                    res.error = res.body
-                callback res.error
+            handleResponse res, callback, "batchDelete"
 
     batchMove: (target, from, to, callback) ->
         body = _.extend {from, to}, target
         request.put "messages/batchMove"
         .send body
         .end (res) ->
-            if res.ok
-                callback null, res.body
-            else
-                if res.body?.name?
-                    res.error = res.body
-                callback res.error
+            handleResponse res, callback, "batchMove"
 
     createAccount: (account, callback) ->
-
         # TODO: validation & sanitization
-
         request.post 'account'
         .send account
         .set 'Accept', 'application/json'
         .end (res) ->
-            if res.ok
-                callback null, res.body
-            else
-                console.log "Error in createAccount", account, res.body?.error
-                callback res.body, null
+            handleResponse res, callback, "createAccount", account
 
     editAccount: (account, callback) ->
 

        .send account
        .set 'Accept', 'application/json'
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                console.log "Error in checkAccount", res.body
                callback res.body, null

    removeAccount: (accountID) ->

        request.del "account/#{accountID}"
        .set 'Accept', 'application/json'
        .end (res) -> # nothing

    accountDiscover: (domain, callback) ->

        request.get "provider/#{domain}"
        .set 'Accept', 'application/json'
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                callback res.body, null

    search: (url, callback) ->
        request.get url
        .set 'Accept', 'application/json'
        .end (res) ->
            if res.ok
                callback null, res.body
            else
                console.log "Error in search", res.body?.error
                callback res.body, null

    refresh: (hard, callback) ->
        url = if hard then "refresh?all=true"

        .send rawAccount
        .set 'Accept', 'application/json'
        .end (res) ->
            handleResponse res, callback, "editAccount", account

    checkAccount: (account, callback) ->

