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"); \
|
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);
|
||||||
|
|
||||||
|
|
Reference in New Issue