refactor(example/main/c): Deduplicate error handling function

This commit is contained in:
Benedikt Peetz 2024-03-24 17:52:36 +01:00
parent 86d00608eb
commit cd5b0c9ee4
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
1 changed files with 14 additions and 14 deletions

View File

@ -36,6 +36,16 @@
printf ("\n\33[0m"); \ printf ("\n\33[0m"); \
fflush (stdout); 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 int
plugin_main () plugin_main ()
{ {
@ -47,25 +57,15 @@ plugin_main ()
// //
// A error will be signaled by a 0 return code, then the error can be // 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 // 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)) if (!outstanding (NULL))
{ 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);
}
println ("Saying hi!"); println ("Saying hi!");
string_t hi; string_t hi;
if (!one.hi (&hi, "Adam")) if (!one.hi (&hi, "Adam"))
{ 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);
}
println ("Rust returned: %s", hi); println ("Rust returned: %s", hi);
string_free (hi); string_free (hi);