Refactor code to automatically send an error back to the requester

in case a handler returns an error object