0

I have XML error returned from the server, which the snippet of the interest part of the error is like this:

... <p class="break-long-words trace-message">SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column &quot;name&quot; violates not-null constraint<br /> DETAIL: Failing row contains (165, null, null, 11, null, 2018-04-19 03:01:48, null, f, 6).</p> ...

Because this happens often during development, I want to make it easier for me to scan for this particular error message and report it to the backend team. So from the error like above, I conclude that I can confidently get the substring that begins with SQLSTATE and ends before </p>. So the cleaned error will be:

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column &quot;name&quot; violates not-null constraint<br /> DETAIL: Failing row contains (165, null, null, 11, null, 2018-04-19 03:01:48, null, f, 6).

The question is, how to do that in Swift? The XML error is not just this only. I just cut out the point of interest. But there will be a plethora of <p></p> tags.

Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124

1 Answers1

0

Using extension found in https://stackoverflow.com/a/32306142/300392 I have implemented my own solution:

if xml.contains("</p>") {
    let beginIndex = xml.index(of: "SQLSTATE")!
    let result1 = String(xml[beginIndex...]);

    let endIndex = result1.index(of: "</p>")!;
    let endIndexBefore = result1.index(before: endIndex);
    let result2 = String(result1[...endIndexBefore]);

    print ("SQL ERROR: \(result2)");
}
Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124