Code Monkey home page Code Monkey logo

Comments (10)

ernho avatar ernho commented on August 14, 2024 1

@graut I am getting no Sorry, duplicate contact found with the same email address. I got {"error":"Unexpected token S","stack":"SyntaxError: Unexpected token S\n and was wondering what is happening.

You guys should handle better errors. This part
var contacts = JSON.parse(body);
can always throw an error. can you change your code?

Maybe I am going to refactor your code.

from nodejs.

graut avatar graut commented on August 14, 2024

Hi Please use status code to check status is not 200 and do action based on that. For example please test with below code in ContactAPI.prototype.add function on success -

`ContactAPI.prototype.add = function add(contact, success, failure) {
var options = this.getOptions();
options.path = '/dev/api/contacts';
options.method = 'POST';
options.headers['Content-Type'] = 'application/json';

var post = https.request(options, function(resp) {
    resp.setEncoding('utf8');
    var body = "";
    resp.on('data', function(data) {
        body += data;
    });
    resp.on('end', function() {
        if (success) {
            try {
                console.log(resp.statusCode);
                var statusCode = resp.statusCode;
                if (statusCode != 200){
                    console.log(body)
                    return;
                }
                var contacts = JSON.parse(body);
                success(contacts);
            } catch (ex) {
                failure(ex);
            }
        }
    });
    resp.on('error', function(e) {
        if (failure) {
            failure(e);
        }
    });
});

try {
    var data = JSON.stringify(contact);
    post.write(data);
    post.end();
} catch (ex) {
    failure(ex);
}

};`

from nodejs.

mete89 avatar mete89 commented on August 14, 2024

What happened to duplicate email error? This time we don't get any error. It can create contacts with same email accounts twice.

from nodejs.

graut avatar graut commented on August 14, 2024

Hi @mete89

I am getting duplicate contact with same email address as you can see in below link image.

https://snag.gy/dCEFhP.jpg

First time it is creating contact with unique email and second time we are checking email and if found then we throwing 400 status code with error message : duplicate contact

In Agile CRM unique email only allowed.

You can see my sample code which I tested and you can also check with your credential

In same folder

  1. agilecrm.js
    https://gist.github.com/graut/8107a3213b8a50028dec1b692d98f983
  2. test.js
    https://gist.github.com/graut/987c6223d8c2b0effce890c301190946

Please let me know if you need any further information.

Thanks

from nodejs.

graut avatar graut commented on August 14, 2024

Hi @ernho ,

Could you please let us know what is the status code printing for your request. Please check below agilecrm.js file with small modification which will catch 400 error and show duplicate error message.

https://snag.gy/mTrQs3.jpg

We will update npm file for few method with error code and message. Please note that for 400 error code you can get proper message but for 500 server error as we are directly throwing error to user, Message won't we formatted and may be in the form of Java exception.

If you will pass correct format of JSON to Agile CRM then there is very rare chance of getting 500 server error.

Please let us know in case need further help.

Thanks

from nodejs.

ernho avatar ernho commented on August 14, 2024

Hi @graut

I am getting this error when I tried to insert a existing contact. Maybe it was not clear but you should catch the error thown by the parse function:

var contacts = JSON.parse(body);

JSON.parse() thows this {"error":"Unexpected token S","stack":"SyntaxError:.. error so this is not good

      if (success) {
            try {
                var contacts = JSON.parse(body);
                success(contacts);
            } catch (ex) {
                failure(ex);
            }
        }

In your modification when body is empty it will trow again the same error

from nodejs.

graut avatar graut commented on August 14, 2024

Hi @ernho,

Yes you are write. I will add some code for checking exception thrown by parse too.

Thank you for you suggestion. We will implement this changes very soon for exception handling in node.js files.

Meanwhile please add try catch for parse and our suggestion for error handling. You can add above changes to every method which are frequently used by your application.

Please let us know in case need further information.

from nodejs.

tilman avatar tilman commented on August 14, 2024

What is the current status about improved Error handling? I wasted a lot hours because the miss leading error messages. Also if your function receive success and error functions, errors should always be returned over the error and not the success function, which was not the case for me.

from nodejs.

graut avatar graut commented on August 14, 2024

Hi @tilman,

Could you please let me know which package version of npm (agile_crm) you are using ? In our latest code we have simple error handling to show status code and message. Some time while parsing empty data there are possibility of throwing exception and that is why we are also throwing exception from success code.

Since this code is sample for testing all methods, It is strongly recommended to modify your method as accordance to your requirement and use.

In case you need more information regarding Agile CRM REST API please check below main reference :

https://github.com/agilecrm/rest-api

from nodejs.

rammohan-agile avatar rammohan-agile commented on August 14, 2024

Hi all Please find the latest code we have updated error handling code, if you are facing any issues please let us know ,

In case you need more information regarding Agile CRM REST API please check below main reference :

https://github.com/agilecrm/rest-api

Thanks,
Agile crm support team.

from nodejs.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.