added comments and fixed exception json in middleware