Author Topic: Plugin Error Catching (DB Connection)  (Read 8530 times)

Offline threeS

  • Multiple posting newcomer
  • *
  • Posts: 36
Plugin Error Catching (DB Connection)
« on: December 14, 2014, 04:21:00 pm »
Good day to all!

I have these line in my developed plugin for codeblocks IDE, the connection for my postgresql database.
Quote
< . . . codes . . . >
     conn = PQconnectdb("dbname=dbname user=postgres password=postgres host=127.0.0.1 port=5432");
< . . . codes . . . >

I want to create a somewhat catch C++ code that catch if the connection fail and pops-up the fail error and not close the IDE or not crash it.

Can anybody help me please?  :(
God Bless and Merry Christmas to All!

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Plugin Error Catching (DB Connection)
« Reply #1 on: December 14, 2014, 04:27:23 pm »
I want to create a somewhat catch C++ code that catch if the connection fail and pops-up the fail error and not close the IDE or not crash it.
You can simply use C++ try catch statement in your plugin source.
Code
your_plugin_event_handler_function()
{
  try
  {
    connect to your database
  }
  catch (error)
  {
    show a message box
  }
}
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline threeS

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: Plugin Error Catching (DB Connection)
« Reply #2 on: December 14, 2014, 04:39:12 pm »
Thank you for that,.
Yes i can create like,
Quote
catch (error)
  {
    show a message box
  }
}
because id tried something like this
Quote
if(PQstatus(conn) != CONNECTION_OK) {
        Manager::Get()->GetLogManager()->Log(_("Database connection failed!"));
}
it print the error in the IDE console and crash the IDE with an error something like "Aborted (core dumped)".

I want in my "show a message box" a pop-up like error message and will not crash my IDE.,

Is that possible guys?  :(
please help me :)

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Plugin Error Catching (DB Connection)
« Reply #3 on: December 14, 2014, 05:33:58 pm »
For sure it is possible.
You have to read the documentation for the API you're trying to use. Learn how to detect and handle errors, change your code.
If there are crashes use a debugger to try to understand why.

Most of the times it is dereferencing null/invalid pointers.

p.s. This has nothing to do with C::B, it is just a generic programming problem.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline threeS

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: Plugin Error Catching (DB Connection)
« Reply #4 on: December 15, 2014, 06:24:13 pm »
Yeah, i got you point sir.
I can handle the error catching, but what i'm asking is how to display the error like a pop-up on codeblocks IDE.
So, maybe a plugin warning pop-up related something?  :(

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Plugin Error Catching (DB Connection)
« Reply #5 on: December 15, 2014, 09:17:31 pm »
Either use the cbMessageBox function, the InfoWindow class or the LogManager.
It is up to you to decide what you should do with your plugin.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline threeS

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: Plugin Error Catching (DB Connection)
« Reply #6 on: December 17, 2014, 05:35:13 am »
thank you so much,.
i will give a shot for that  :)

Offline threeS

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: Plugin Error Catching (DB Connection)
« Reply #7 on: December 17, 2014, 06:57:04 pm »
Additional guys.,
How to catch errors inside a plugin that prevent the IDE to crash?  :(

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Plugin Error Catching (DB Connection)
« Reply #8 on: December 17, 2014, 11:27:00 pm »
Use a debugger to find the cause, then fix the bug.

If you read the c++ specification you'll find that you cannot catch segmentation faults/access violations as exceptions!
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline threeS

  • Multiple posting newcomer
  • *
  • Posts: 36
Re: Plugin Error Catching (DB Connection)
« Reply #9 on: December 18, 2014, 07:31:20 am »
that is why . :(

I have another idea, maybe i can disable the plugin, so that the IDE will not crash.
How can it be possible?