BugFix 884342

added a custom error function, a function for error outputs and changed middleware to use new functions