michaelcarter / mixpanel-data-export-js

Mixpanel data export js
91 stars 38 forks source link

Trap errors from the export endpoint #31

Open michaelcarter opened 5 years ago

michaelcarter commented 5 years ago

Mixpanel's non-export API endpoints give 200 status codes with a JSON error when something goes wrong, but the export endpoint gives a non-200 status code with a string. The library was trying to parse this into JSON, failing awkwardly and blowing up.

This isn't really a full fix, as ideally we'd have promises rejecting properly with errors when they occur, but it should at least make the export API's behaviour consistent with behaviour of the other methods and give a similarly structured { error: "foobar" } object

GeoffreyPlitt commented 5 years ago

@michaelcarter

Is this working? I switched to this branch and still get untrapped errors.

Or if this is working, are you going to merge it?

GeoffreyPlitt commented 5 years ago

Here's what my error logs look like: undefined:1 [terminated early] ^ SyntaxError: Unexpected token e in JSON at position 2 at Object.parse (native) at MixpanelExport._parseResponse (/app/node_modules/mixpanel-data-export/src/mixpanel_data_export.js:180:17) 21) 25) at setState (/app/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:614:14) at IncomingMessage.<anonymous> (/app/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:447:13) 20) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12) at _combinedTickCallback (internal/process/next_tick.js:80:11) at process._tickDomainCallback (internal/process/next_tick.js:128:9)

michaelcarter commented 5 years ago

@GeoffreyPlitt I'll need to put aside some time to look back into this for you.

I'm not saying it's the case here, but historically I've had issues with Mixpanel not consistently returning valid JSON (or even consistent error messages), their export API is prone to breaking changes.

Could you give me some info on the request you're making so I can recreate this locally please? I can then either put in a fix, or add some pointers on here for someone else to put in a PR.

GeoffreyPlitt commented 5 years ago

Gotcha. We're doing mixpanel_exporter.export() with an opts object like this:

{
        event: ['ev1', 'ev2', 'ev3'],
        from_date: '2019-03-01',
        to_date: '2019-03-05',
        where: `"44845" in properties["field1"]`
      }
GeoffreyPlitt commented 5 years ago

Another one

Mar 05 13:12:41 stardust-backend app/trailer_insights_prefetch.1: Received 2122 raw datapoints from Mixpanel. Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: undefined:1 Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: [{"event":"impression","properties":{"time":1543155025,"distinct_id":"c92efae6-b148-45ee-8e42-f2cdabd275b4","$city":"Ashburn","$region":"Virginia","cameFrom":"home_daily_row","dob":"1997-05-04T21:00:00","gender":"MALE","mediaId":524247,"mediaType":"movie","mp_country_code":"US","mp_lib":"node","mp_processing_time_ms":1543183825874,"trailerId":"f25b9757-2b32-45f7-8e26-e032d1316762","username":"JPartin"}},{"event":"impression","properties":{"time":1543155034,"distinct_id":"c6568763-4666-45f9-8bd5-fbf08a660f71","$city":"Ashburn","$region":"Virginia","cameFrom":"home_daily_row","dob":"1978-11-19T21:00:00","gender":"MALE","mediaId":524247,"mediaType":"movie","mp_country_code":"US","mp_lib":"node","mp_processing_time_ms":1543183835074,"trailerId":"f25b9757-2b32-45f7-8e26-e032d1316762","username":"kenw420"}},{"event":"impression","properties":{"time":1543155038,"distinct_id":"b37000b5-8762-4c0b-90fc-dddb5b2c3949","$city":"Hillsboro","$region":"Oregon","cameFrom":"home_daily_row","dob":"1997-09-16T00:00 Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: SyntaxError: Unexpected token ] in JSON at position 15135 Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at Object.parse (native) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at MixpanelExport._parseResponse (/app/node_modules/mixpanel-data-export/src/mixpanel_data_export.js:180:17) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at request.onload (/app/node_modules/mixpanel-data-export/src/mixpanel_data_export.js:151:21) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at dispatchEvent (/app/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:591:25) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at setState (/app/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:614:14) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at IncomingMessage.<anonymous> (/app/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:447:13) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at emitNone (events.js:91:20) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at IncomingMessage.emit (events.js:185:7) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at endReadableNT (_stream_readable.js:974:12) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at _combinedTickCallback (internal/process/next_tick.js:80:11) Mar 05 13:16:00 stardust-backend app/trailer_insights_prefetch.1: at process._tickDomainCallback (internal/process/next_tick.js:128:9) Mar 05 13:16:03 stardust-backend app/trailer_insights_prefetch.1: trailer_insights_prefetching: Starting...