refactor(example/main/c): Deduplicate error handling function
This commit is contained in:
parent
86d00608eb
commit
cd5b0c9ee4
|
@ -36,6 +36,16 @@
|
|||
printf ("\n\33[0m"); \
|
||||
fflush (stdout);
|
||||
|
||||
void
|
||||
handle_error ()
|
||||
{
|
||||
int error_length = last_error_length ();
|
||||
char *error = malloc (error_length);
|
||||
last_error_message (error, error_length);
|
||||
eprintln ("Encountered error: %s", error);
|
||||
free (error);
|
||||
}
|
||||
|
||||
int
|
||||
plugin_main ()
|
||||
{
|
||||
|
@ -47,25 +57,15 @@ plugin_main ()
|
|||
//
|
||||
// A error will be signaled by a 0 return code, then the error can be
|
||||
// obtained with the `last_error_length` and `last_error_message` functions
|
||||
// (which in our case is wrapped by the `handle_error` function)
|
||||
if (!outstanding (NULL))
|
||||
{
|
||||
int error_length = last_error_length ();
|
||||
char *error = malloc (error_length);
|
||||
last_error_message (error, error_length);
|
||||
eprintln ("Encountered error: %s", error);
|
||||
free (error);
|
||||
}
|
||||
handle_error ();
|
||||
|
||||
println ("Saying hi!");
|
||||
string_t hi;
|
||||
if (!one.hi (&hi, "Adam"))
|
||||
{
|
||||
int error_length = last_error_length ();
|
||||
char *error = malloc (error_length);
|
||||
last_error_message (error, error_length);
|
||||
eprintln ("Encountered error: %s", error);
|
||||
free (error);
|
||||
}
|
||||
handle_error ();
|
||||
|
||||
println ("Rust returned: %s", hi);
|
||||
string_free (hi);
|
||||
|
||||
|
|
Reference in New Issue