Changed database logic to aggregate errors

* Used aggregate call on database to group errors by errorCode