const stringify = require('json-stringify-safe');
const isProd = process.env.NODE_ENV === 'production';

const titleToReadable = function(inputString) {
  const regEx = /.*\.([^.]*)/;
  if (!inputString) {
    return undefined;
  }
  return inputString.substring(inputString.indexOf(':') + 2).split(', ').map((eTitle) => {
    const match = regEx.exec(eTitle);
    return match ? match[1] : eTitle;
  }).join(', ');
};

module.exports = {
  'application/vnd.api+json': function jsonapiFormatter(req, res, body, next) {
    const response = {
      meta: {
        id: req.id(),
      },
    };

    if (res.meta) {
      Object.assign(response.meta, res.meta);
    }

    if (res.links) {
      response.links = Object.assign({}, res.links);
    }

    if (!isProd) {
      response.meta.timers = req.timers;
    }

    if (body instanceof Error) {
      // check for error message
      res.statusCode = body.statusCode || body.code || body.status_code || 500;
      response.errors = [];

      let err;
      if (typeof body.toJSON === 'function') {
        err = body.toJSON();
        const title = typeof body.generateMessage === 'function' ? body.generateMessage() : (err.message || err.text);
        response.errors.push({
          status: body.name,
          code: res.statusCode,
          title: titleToReadable(title),
          detail: err.errors,
        });
      } else {
        response.errors.push({
          status: body.name || 'InternalServerError',
          code: res.statusCode,
          title: titleToReadable(body.toString()),
          detail: body.reason || body.errors || {},
          stack: isProd ? undefined : (typeof body.stack === 'string' ? body.stack.split('\n') : body.stack),
        });
      }

      ///////////////////// readable format of title


      ///////////
    } else if (Buffer.isBuffer(body)) {
      response.data = body.toString('base64');
    } else if (body) {
      response.data = body;
    }

    const data = stringify(response);
    res.setHeader('Content-Type', 'application/vnd.api+json');
    res.setHeader('Content-Length', Buffer.byteLength(data));

    return next(null, data);
  },
};

@@ -134,24 +134,237 @@
 tion
-(inputString) %7B%0A
+ titleToReadable(name, inputString) %7B%0A  if (name === 'HttpStatusError' %7C%7C name === 'ValidationError') %7B%0A    const colonPosition = inputString.indexOf(':');%0A    const cutBeginning = colonPosition === -1 ? 0 : colonPosition + 2;%0A  
   co
@@ -390,16 +390,18 @@
 %5E.%5D*)/;%0A
+  
   if (!i
@@ -414,24 +414,26 @@
 ring) %7B%0A    
+  
 return undef
@@ -438,20 +438,24 @@
 efined;%0A
+  
   %7D%0A
+  
   return
@@ -481,36 +481,20 @@
 ing(
-inputString.indexOf(':') + 2
+cutBeginning
 ).sp
@@ -521,16 +521,18 @@
 e) =%3E %7B%0A
+  
     cons
@@ -561,24 +561,26 @@
 Title);%0A    
+  
 return match
@@ -601,16 +601,18 @@
 eTitle;%0A
+  
   %7D).joi
@@ -620,16 +620,20 @@
 (', ');%0A
+  %7D%0A
 %7D;%0A%0Amodu
@@ -1534,16 +1534,27 @@
 eadable(
+body.name, 
 title),%0A
@@ -1756,24 +1756,35 @@
 eToReadable(
+body.name, 
 body.toStrin

Check name