diff --git a/cmd/main/main.go b/cmd/main/main.go index a88b69a..893958d 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -51,7 +51,8 @@ func sendMessage(c *gin.Context) { message.Priority, message.Event, message.Val, message.Msg) url := getURL(message.URL) resp := internal.Send(url, msg) - c.String(http.StatusOK, resp) + errMsg := internal.GetErrMsg(resp) + c.JSON(http.StatusOK, gin.H{"error": errMsg}) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } diff --git a/internal/codec.go b/internal/codec.go index e0ba898..bb671ca 100644 --- a/internal/codec.go +++ b/internal/codec.go @@ -84,6 +84,7 @@ const ( ` msgExp = `.*<\/ns1:string>` + errExp = `` ) // CreateSend xml string for sending @@ -122,6 +123,17 @@ func GetMsgs(soap string) []string { return Map(msgs, split) } +//GetErrMsg get error message +func GetErrMsg(soap string) string { + r, _ := regexp.Compile(errExp) + msg := r.FindAllString(soap, -1) + if len(msg) > 0 && len(msg[0]) > 40 { + size := len(msg[0]) + return msg[0][54 : size-15] + } + return msg[0] +} + func split(msg string) string { if len(msg) > 12 { return msg[12 : len(msg)-13] diff --git a/internal/codec_test.go b/internal/codec_test.go index 1764cb7..f5382e7 100644 --- a/internal/codec_test.go +++ b/internal/codec_test.go @@ -92,6 +92,8 @@ const ( ` Msg string = ` AODB 820830 20180527124442 FLOP ESTT 10725849 CA-CA4242-A-27MAY181955-D 27MAY181945 ` + + ErrMsg string = `soap:Serverinvalid route envent id 20Can not find the event [FLOP-ESTT-ATC-ALL1]` ) func TestSend(t *testing.T) { @@ -118,3 +120,9 @@ func TestGetMsg(t *testing.T) { assert.Equal(2, len(msgs), "should get 2 message") assert.Equal(Msg, msgs[0], "message") } + +func TestGetErrMsg(t *testing.T) { + assert := assert.New(t) + errMsg := GetErrMsg(ErrMsg) + assert.Equal("Can not find the event [FLOP-ESTT-ATC-ALL1]", errMsg) +}