added a custom error function, a function for error outputs and changed middleware to use new functions
Merge pull request #285 from elnushaj/BugFix884342

BugFix 884342