fixed the segfault when handling errors