manticoresoftware / manticoresearch-typescript

Official TypeScript client for Manticore Search
MIT License
8 stars 3 forks source link

SearchApi does not handle manticore error responses #8

Closed Rich5 closed 7 months ago

Rich5 commented 7 months ago

Maybe this behavior is intended but I wanted to ask to be sure. Looking at the simple search example below from the README I made the assumption (perhaps incorrectly) that errors returned from Manticore would be handled by the catch block, but that only appears to be the case when there is something like a network error that gets thrown. However, there is no error thrown when Manticore returns an error object like in the case of a syntax error.

try {

<snip>

    const searchApi = new SearchApi();
    const searchResponse = await searchApi.search({
      index: "test",
      query: { query_string: "Title 1" },
    });
    console.info("Search response:", JSON.stringify(searchResponse, null, 2));

  } catch (error) {
    const errorResponse =
      error instanceof ResponseError ? await error.response.json() : error;
    console.error("Error response:", JSON.stringify(errorResponse, null, 2));
  }

Example error response during a syntax error:

[
    {
      total: 0,
      warning: '',
      error: "index test_table: query error: P08: syntax error, unexpected '~' near '~'"
    }
]

If this is intended behavior then it looks like the SearchResponse interface does not account for Manticore errors. As a result I ended up extending the SearchResponse interface, like shown below, in my app.

export interface ErrorMessage {

    total: number
    warning: string
    error: string
}

export interface SearchResponseWithError extends SearchResponse {

    message?: ErrorMessage | null

} 

Then I just handled the error messages specifically in my results handling code. If this in intended then maybe just a small update to the example code would clarify usage better. Another option is to have SearchApi throw errors.

Nick-S-2018 commented 7 months ago

No, this behavior isn't intended. We're aware of the existing discrepancy between the error response format currently specified for the client and the formats Manticore can possibly return. We're going to fix this issue in the next client release which is expected soon.

Nick-S-2018 commented 7 months ago

Related to https://github.com/manticoresoftware/openapi/issues/16

Rich5 commented 7 months ago

Thanks! I'd be happy to test the error handling in the next release if that's useful.