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