JavaScript if/else logic for Zapier authentication validation

Need help with conditional logic in Zapier app development

I’m still learning JavaScript and building my first Zapier integration. I have an authentication test that should fail when wrong credentials are provided, but I can’t figure out how to make it work properly.

My test setup:

const should = require('should');
const zapier = require('zapier-platform-core');
const MyApp = require('../../app');
const tester = zapier.createAppTester(MyApp);

describe('Auth Test - Fetch Categories', () => {
  zapier.tools.env.inject();

  it('should return data array', finished => {
    const config = {
      authData: { token: process.env.AUTH_TOKEN },
      inputData: {}
    };

    tester(MyApp.triggers['fetchcategories'].operation.perform, config)
      .then(data => {
        data.includes('categoryId');
        finished();
      })
      .catch(data);
  });
});

Success response example:

{"categoryId":2847,"title":"Active Users","total":15}

Error response example:

{"STATUS":"ERROR","MESSAGE":"Authentication failed"}

My trigger function:

const { substituteVars } = require('../helpers');

const fetchCategories = (z, bundle) => {
  let endpoint = 'https://myapi.example.com/v1/categories?token={{token}}';
  endpoint = substituteVars(endpoint, bundle);

  const request = z.request({ url: endpoint });
  return request.then(response => {
    response.throwForStatus();
    return z.JSON.parse(response.content);
  });
};

module.exports = {
  key: 'fetchcategories',
  noun: 'Category',
  display: {
    label: 'Fetch Categories',
    description: 'Loads available categories from API',
    hidden: true,
    important: false
  },
  operation: {
    inputFields: [
      {
        key: 'category',
        label: 'Category Filter',
        type: 'string',
        required: false
      }
    ],
    outputFields: [
      {
        key: 'total',
        type: 'string'
      },
      {
        key: 'categoryId',
        type: 'string',
        label: 'Category ID'
      },
      {
        key: 'title',
        type: 'string',
        label: 'Category Title'
      }
    ],
    perform: fetchCategories,
    sample: { total: 156, categoryId: 19203, title: 'Active Users' }
  }
};

When testing authentication in Zapier’s interface, I want the auth to fail and display the MESSAGE field from the error response. What’s the correct way to handle this conditional behavior?

You need to handle the error response before throwForStatus() gets called. Your API returns a 200 status with an error payload, so throwForStatus() won’t catch it automatically.

Modify your fetchCategories function to check for the error condition first:

const fetchCategories = (z, bundle) => {
  let endpoint = 'https://myapi.example.com/v1/categories?token={{token}}';
  endpoint = substituteVars(endpoint, bundle);

  const request = z.request({ url: endpoint });
  return request.then(response => {
    const data = z.JSON.parse(response.content);
    
    if (data.STATUS === 'ERROR') {
      throw new z.errors.Error(data.MESSAGE, 'AuthenticationError');
    }
    
    response.throwForStatus();
    return data;
  });
};

This way Zapier will catch the authentication failure and show your custom error message to users during connection tests.

Had the same issue with my first Zapier app. Your API sends HTTP 200 even for auth errors, so throwForStatus() won’t catch them. You’ve got to check the response content first.

Try this:

return request.then(response => {
  response.throwForStatus();
  const data = z.JSON.parse(response.content);
  
  if (data.STATUS && data.STATUS === 'ERROR') {
    throw new Error(data.MESSAGE || 'Authentication failed');
  }
  
  return data;
});

Also update your test’s catch handler to finished instead of data. This approach works great for APIs that return structured errors instead of proper HTTP codes.

Exactly right, but move the JSON parsing after the status check or you’ll get parsing errors on weird responses. Also, your test needs .catch(finished) not .catch(data) - otherwise it won’t work when auth fails.