2

I am throwing a custom exception as below to UI

StringBuilder errorMessage = new StringBuilder("Error One Message"); 
String newlinetest = System.getProperty("line.separator");
                  // String sep = System.line.separator;
String secondErrorMessage = "Error Two message";
throw new ErrorWhileCreatingMessageException(errorMessage+"\r\n"+secondErrorMessage);

I need to add new line between two error messages before throwing. I tried \n, \r\n and line. separator it didn't work. Any help on this is much appreciated

I am using Angular JS in the UI. I am displaying as below

<div ng-show="error != null">
    <div class="alert alert-danger" role="alert" ng-bind="error"></div>
</div>
6
  • 3
    What is your UI? Commented Jun 29, 2017 at 20:18
  • What are you seeing? Commented Jun 29, 2017 at 20:19
  • \n should create a new line for display (but not necessarily for a text file) -- more importantly -- how are you displaying the exception String? Best to create and post a valid minimal reproducible example / sscce. Commented Jun 29, 2017 at 20:19
  • @Makoto i am seeing Error One Message Error Two message without any break in the middle Commented Jun 29, 2017 at 20:19
  • 1
    @Kiran what is your UI??? Commented Jun 29, 2017 at 20:22

3 Answers 3

2

Why dont you just read the content of the div you are inserting the error message in, and find/replace \r\n with a <br>

or just straight up try a <br> in that error message instead of \r\n, does that help?

Sign up to request clarification or add additional context in comments.

4 Comments

If I add <br/>. It is displaying as Error One Message <br/> Error Two message.It is not breaking the word
Angular isn't going to allow you to place arbitrary HTML in a response and suddenly render it.
its being put in a div right??? so after you place the message contents in your div, replace the \r\n. Angular or whatever framework your using may be escaping the string for you and thats why you cant put in any special characters. can you tell us what code is in the div AFTER the message is displayed?
@MaxAlexanderHanna I used ng-bind-html to display the string with break tag in it and it worked.
1

If you're showing the result in HTML try replacing \r\n with < br>

4 Comments

If I add <br/>. It is displaying as Error One Message <br/> Error Two message.It is not breaking the word
Angular isn't going to allow you to place arbitrary HTML in a response and suddenly render it.
you could atleast try to vary from my answer a little bit :)
Sorry Max, but when I started writing your response wasn't here still, also yours is more complete and better explained than mine. Again, sorry if I hurted your ego (?)
1

The idiomatic thing to do here is to return a proper error object from your rest service when an error happens, and split out the error type from the error message. This will require a new class and require you to send that over the wire, but you'll find it easier to deal with as you'll be able to properly populate values for error.type and error.message.

By default, Angular isn't going to allow you to render raw HTML in responses unless you regard it as unsafe. In 99% of the cases I've seen with Angular, that's exactly what you don't want to do.

Alternatively...since exceptions are meant for you as a developer and error messages are meant for consumers, you may want to rethink what you're sending. You don't want to send across the stack trace or anything too specific to the end user, since they're not going to understand it. It would be best to select a generic message which explains what the issue is in general, and what they're doing to fix it. For some inspiration, Stack Overflow's error page does a decent job of keeping us as clients shielded from the actual error.

3 Comments

I am returning only the message from the Exception to UI. I am not returning type as below public ErrorWhileCreatingMessageException(String message) { super(message); }
Thank you so much for your recommendation. I tried ng-bind-html and ng-bind-html-unsafe but it didn't work. Do you have any suggestions for me.
As stated...don't pass an entire exception. Pass only the messages you want the user to see in an object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.