function generateError (definition, definitionVersion, error) {
  let ajv = new Ajv({
      schemaId: 'auto',
      strict: false
    }),
    validate,
    valid = true;

  // Log original error for better obseravibility in case of User errors
  console.error(error);

  addFormats(ajv);

  if (definitionVersion === openapi3Utils.version) {
    validate = ajv.compile(openapi3Schema);
  }
  else if (definitionVersion === swagger2Utils.version) {
    validate = ajv.compile(swagger2Schema);
  }

  validate && (valid = validate(definition));

  if (valid) {
    if (error instanceof Error) {
      return error;
    }

    const errorMessage = typeof error === 'string' ? error : _.get(error, 'message', 'Failed to generate collection.');

    return new Error(errorMessage);
  }
  else {
    return new UserError(constructErrorMessage(validate.errors[0]), error);
  }
}
Validates given OpenAPI specification against JSON schema of OpenAPI spec using AJV and
generates UserError or Unhandled error depending upon type of error.

@param {Object} definition - Parsed OAS definition
@param {string} definitionVersion - Corresponding Definition version
@param {*} error - Original Error object
@returns {*} - Generated Error object