Skip to content

Commit

Permalink
Add specific exception for invalid JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmont-dev committed Aug 15, 2024
1 parent a97aa99 commit d93a7b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions include/ollama.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ namespace ollama
const char* what() const noexcept override { return message.c_str(); }
};

class invalid_json_exception : public ollama::exception { public: using exception::exception; };

class image {
public:
image(const std::string base64_sequence, bool valid = true)
Expand Down Expand Up @@ -304,7 +306,7 @@ namespace ollama

if ( json_data.contains("error") ) error_string =json_data["error"].get<std::string>();
}
catch(...) { if (ollama::use_exceptions) throw new ollama::exception("Unable to parse JSON string:"+this->json_string); valid = false; }
catch(...) { if (ollama::use_exceptions) throw ollama::invalid_json_exception("Unable to parse JSON string:"+this->json_string); valid = false; }
}

response() {json_string = ""; valid = false;}
Expand Down Expand Up @@ -437,7 +439,7 @@ class Ollama
partial_responses->clear();
on_receive_token(response);
}
catch (...) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }
catch (const ollama::invalid_json_exception& e) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }

return true;
};
Expand Down
6 changes: 4 additions & 2 deletions singleheader/ollama.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34885,6 +34885,8 @@ namespace ollama
const char* what() const noexcept override { return message.c_str(); }
};

class invalid_json_exception : public ollama::exception { public: using exception::exception; };

class image {
public:
image(const std::string base64_sequence, bool valid = true)
Expand Down Expand Up @@ -35094,7 +35096,7 @@ namespace ollama

if ( json_data.contains("error") ) error_string =json_data["error"].get<std::string>();
}
catch(...) { if (ollama::use_exceptions) throw new ollama::exception("Unable to parse JSON string:"+this->json_string); valid = false; }
catch(...) { if (ollama::use_exceptions) throw ollama::invalid_json_exception("Unable to parse JSON string:"+this->json_string); valid = false; }
}

response() {json_string = ""; valid = false;}
Expand Down Expand Up @@ -35227,7 +35229,7 @@ class Ollama
partial_responses->clear();
on_receive_token(response);
}
catch (...) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }
catch (const ollama::invalid_json_exception& e) { /* Partial response was received. Will do nothing and attempt to concatenate with the next response. */ }

return true;
};
Expand Down

0 comments on commit d93a7b3

Please sign in to comment.