xmppo / node-expat

libexpat XML SAX parser binding for node.js
https://github.com/xmppo/node-expat
MIT License
385 stars 97 forks source link

stop / resume fails with "parser suspended" error #187

Open alexey-baranov opened 6 years ago

alexey-baranov commented 6 years ago

hello! hire is my simple code. it fails with error string "parser suspended"

      parser.on("error", err => {
        logger.error(err)
        //rej(e);
      })

      parser.on("startElement", (name, attrs) => {
        if (name == 'Object' && attrs.LIVESTATUS == 1) {
          logger.debug("stopping...")
          parser.stop()
          models.Zemla.create({
            AOGUID: attrs.AOGUID,
            PARENTGUID: attrs.PARENTGUID ? attrs.PARENTGUID : null,
            SHORTNAME: attrs.SHORTNAME,
            name: attrs.OFFNAME,
            level: attrs.AOLEVEL,
            parent_id: null,
            country_id: RUSSIA
          })
            .then(() => {
              parser.resume()
            })
            .catch(err=>{
              logger.error(attrs, err)
              parser.resume()
            })
        }
      })

      fs.createReadStream(path)
        .pipe(parser);

output

[2018-05-04 00:01:52.930] [DEBUG] FIASImporter - stopping... [2018-05-04 00:02:02.873] [ERROR] FIASImporter - parser suspended [2018-05-04 00:02:02.945] [INFO] console - Executing (default): INSERT INTO "Zemla" ("id","AOGUID","PARENTGUID","SHORTNAME","name","level","created_at","updated_at","parent_id","country_id") VALUES (DEFAULT,'d8327a56-80de-4df2-815c-4f6ab1224c50',NULL,'Респ','Адыгея','1','2018-05-03 19:02:02.856 +00:00','2018-05-03 19:02:02.856 +00:00',NULL,1002) RETURNING ; [2018-05-04 00:02:02.970] [INFO] console - Executing (default): INSERT INTO "ZemlaTree" ("id","deep","created_at","updated_at","menshe_id","bolshe_id") VALUES (DEFAULT,0,'2018-05-03 19:02:02.960 +00:00','2018-05-03 19:02:02.960 +00:00','994251','994251') RETURNING ;

as you can see after first parser.stop() error thrown

georgyfarniev commented 5 years ago

Yes, I also find it strange, why stop() cause this error? This is not normal behavior I think.

georgyfarniev commented 5 years ago

I will try to fix this bug and create PR

georgyfarniev commented 5 years ago

Looks like this is how libexpat works, it notify about stopping via error interface function. So best way is to just ignore this error in your handler. However, it's strange and should be described in docs at least...

mzedeler commented 5 years ago

Also, I suspect that standard tools like pipeline or finished from the mississippi toolkit may fail even though there isn't any error. I would consider not emitting this event as an error.