简体   繁体   English

使用curl和elasticsearch进行错误处理

[英]Error handling with curl and elasticsearch

I'm currently developing bash scripts that use elasticsearch and I need a good error-handling. 我目前正在开发使用elasticsearch的bash脚本,并且需要良好的错误处理。 In this situation I try to add a document to elasticsearch and check if the operation succeeded. 在这种情况下,我尝试将文档添加到elasticsearch并检查操作是否成功。

At first I naively tried this : 起初,我天真地尝试了这个:

response=$(curl -XPOST 'http://localhost:9200/indexation/document' -d '
{
  "content":"'"$txt"'",,
  "date_treatment":"'"$(date +%Y-%m-%d)"'"
}') && echo ok || echo fail

But curl doesn't work that way and still returns success (0 - which is actually logical) even though the json request is obviously incorrect (note the double comma on line 3) and elasticsearch displays errors. 但是curl无法以这种方式工作,即使json请求明显不正确(请注意第3行上的双逗号),并且elasticsearch显示错误,curl仍会返回成功(0-实际上是逻辑上的)。

So the answer isn't there. 因此答案不存在。 Now I think I should analyze the variable $response to catch errors (grep ?). 现在,我认为我应该分析变量$ response以捕获错误(grep吗?)。 I post this question to get hints or solutions on the way to do this in a reliable way and to make sure I'm not missing an obvious solution (maybe a curl option I don't know ?). 我发布此问题以获取以可靠方式执行此操作的提示或解决方案,并确保我不会错过任何明显的解决方案(也许我不知道使用curl选项)。

Additional useful things 其他有用的东西

Parsing JSON with Unix tools 使用Unix工具解析JSON

Examples of the content of $response : $ response的内容示例:

success : 成功:

{
    "_id": "AVQz7Fg0nF90YvJIX_2C",
    "_index": "indexation",
    "_shards": {
        "failed": 0,
        "successful": 1,
        "total": 1
    },
    "_type": "document",
    "_version": 1,
    "created": true
}

error : 错误:

{
    "error": {
        "caused_by": {
            "reason": "json_parse_exception: Unexpected character (',' (code 44)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: org.elasticsearch.common.io.stream.InputStreamStreamInput@139163f; line: 3, column: 17]",
            "type": "json_parse_exception"
        },
        "reason": "failed to parse",
        "root_cause": [
            {
                "reason": "json_parse_exception: Unexpected character (',' (code 44)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: org.elasticsearch.common.io.stream.InputStreamStreamInput@139163f; line: 3, column: 17]",
                "type": "json_parse_exception"
            }
        ],
        "type": "mapper_parsing_exception"
    },
    "status": 400
}

A simple workaround is to use the -f/--fail option. 一个简单的解决方法是使用-f/--fail选项。

As per documentation : 根据文档

(HTTP) Fail silently (no output at all) on server errors. (HTTP)在服务器错误时静默失败(根本没有输出)。 This is mostly done to better enable scripts etc to better deal with failed attempts. 这样做主要是为了更好地使脚本等能够更好地处理失败的尝试。 In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). 在正常情况下,当HTTP服务器无法交付文档时,它会返回一个HTML文档,说明这样做(通常还说明原因以及更多内容)。 This flag will prevent curl from outputting that and return error 22. 该标志将阻止curl输出该错误并返回错误22。

This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407). 此方法不是故障安全的,并且有时会漏出不成功的响应代码,尤其是在涉及身份验证时(响应代码401和407)。

example: 例:

response=$(curl -XPOST 'http://localhost:9200/indexation/document' -d '
{
  "content":"'"$txt"'",,
  "date_treatment":"'"$(date +%Y-%m-%d)"'"
}' -f ) && echo ok || echo fail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM