Is using 500 response code for normal app flow viable? Designing popup messages with Angular7+Spring
From Java standpoint using Exceptions to handle EXPECTED outcomes is wrong (Exceptions should be what they are called).
For all my services I've created wrapper mechanism that basically gives details on failure if such happens (all returns are arbitrary Result<?>
). Now I need to display this message on client browser in some popup.
With Angular there is HttpClient that actually supports catching http response errors:
https://angular.io/guide/http#error-handling
Is this a viable way of reporting error from server to client?
Is there a way in Angular to define some extractor that would split responses from backend API?
Say I'd make my whole REST API return bodies:
{
messageType: "", // Success, Failure, Warning
message: "Message",
content: {
...
}
}
And that way I could strip message, messageType
in interceptor, display them in popup, and pass only content
further as body?
spring-boot angular7
add a comment |
From Java standpoint using Exceptions to handle EXPECTED outcomes is wrong (Exceptions should be what they are called).
For all my services I've created wrapper mechanism that basically gives details on failure if such happens (all returns are arbitrary Result<?>
). Now I need to display this message on client browser in some popup.
With Angular there is HttpClient that actually supports catching http response errors:
https://angular.io/guide/http#error-handling
Is this a viable way of reporting error from server to client?
Is there a way in Angular to define some extractor that would split responses from backend API?
Say I'd make my whole REST API return bodies:
{
messageType: "", // Success, Failure, Warning
message: "Message",
content: {
...
}
}
And that way I could strip message, messageType
in interceptor, display them in popup, and pass only content
further as body?
spring-boot angular7
everything you have described is exactly how everyone does it and yet no one feels shame about it. And 500 is ok for that
– NEGR KITAEC
Dec 29 '18 at 21:43
add a comment |
From Java standpoint using Exceptions to handle EXPECTED outcomes is wrong (Exceptions should be what they are called).
For all my services I've created wrapper mechanism that basically gives details on failure if such happens (all returns are arbitrary Result<?>
). Now I need to display this message on client browser in some popup.
With Angular there is HttpClient that actually supports catching http response errors:
https://angular.io/guide/http#error-handling
Is this a viable way of reporting error from server to client?
Is there a way in Angular to define some extractor that would split responses from backend API?
Say I'd make my whole REST API return bodies:
{
messageType: "", // Success, Failure, Warning
message: "Message",
content: {
...
}
}
And that way I could strip message, messageType
in interceptor, display them in popup, and pass only content
further as body?
spring-boot angular7
From Java standpoint using Exceptions to handle EXPECTED outcomes is wrong (Exceptions should be what they are called).
For all my services I've created wrapper mechanism that basically gives details on failure if such happens (all returns are arbitrary Result<?>
). Now I need to display this message on client browser in some popup.
With Angular there is HttpClient that actually supports catching http response errors:
https://angular.io/guide/http#error-handling
Is this a viable way of reporting error from server to client?
Is there a way in Angular to define some extractor that would split responses from backend API?
Say I'd make my whole REST API return bodies:
{
messageType: "", // Success, Failure, Warning
message: "Message",
content: {
...
}
}
And that way I could strip message, messageType
in interceptor, display them in popup, and pass only content
further as body?
spring-boot angular7
spring-boot angular7
asked Dec 29 '18 at 18:42
ErnioErnio
397213
397213
everything you have described is exactly how everyone does it and yet no one feels shame about it. And 500 is ok for that
– NEGR KITAEC
Dec 29 '18 at 21:43
add a comment |
everything you have described is exactly how everyone does it and yet no one feels shame about it. And 500 is ok for that
– NEGR KITAEC
Dec 29 '18 at 21:43
everything you have described is exactly how everyone does it and yet no one feels shame about it. And 500 is ok for that
– NEGR KITAEC
Dec 29 '18 at 21:43
everything you have described is exactly how everyone does it and yet no one feels shame about it. And 500 is ok for that
– NEGR KITAEC
Dec 29 '18 at 21:43
add a comment |
2 Answers
2
active
oldest
votes
A good way to capture all exceptions at service side using @ControllerAdvice and throw the user/feature specific exceptions with the expected exception message and status code in standard structure so that front end can have an exception controller to evaluate this exception message on a fly in popup message dynamic to any message from service.
add a comment |
Since I came to web from pure Java backends - Exceptions in JAVA are bad (generally speaking), why follow same bad practices (using errors as info) when using HTTP?
After some more reading I can say - don't unless its actually an error, e.g:
- 404 if client tries to access entity with ID that is not there.
- 500 when server ACTUALLY can't handle something (actual Exception).
Here is Angular part for my Result<?>
system.
Interceptor:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Globals } from '../globals.service';
@Injectable()
export class PopupInterceptor implements HttpInterceptor {
constructor(private globals: Globals) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.startsWith(this.globals.apiHost)) {
return next.handle(req)
.pipe(
map(e => {
if (e instanceof HttpResponse) {
const response = <Response>e.body;
// use response to do whatever - I am injecting popup service and pushing response.message there for display in component.
return e.clone({ body: response.result });
}
})
);
}
return next.handle(req);
}
}
interface Response {
type: string;
message: string;
result: any;
}
globals.service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class Globals {
apiHost = '/api/';
}
app.module:
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: PopupInterceptor, multi: true }
],
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972370%2fis-using-500-response-code-for-normal-app-flow-viable-designing-popup-messages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A good way to capture all exceptions at service side using @ControllerAdvice and throw the user/feature specific exceptions with the expected exception message and status code in standard structure so that front end can have an exception controller to evaluate this exception message on a fly in popup message dynamic to any message from service.
add a comment |
A good way to capture all exceptions at service side using @ControllerAdvice and throw the user/feature specific exceptions with the expected exception message and status code in standard structure so that front end can have an exception controller to evaluate this exception message on a fly in popup message dynamic to any message from service.
add a comment |
A good way to capture all exceptions at service side using @ControllerAdvice and throw the user/feature specific exceptions with the expected exception message and status code in standard structure so that front end can have an exception controller to evaluate this exception message on a fly in popup message dynamic to any message from service.
A good way to capture all exceptions at service side using @ControllerAdvice and throw the user/feature specific exceptions with the expected exception message and status code in standard structure so that front end can have an exception controller to evaluate this exception message on a fly in popup message dynamic to any message from service.
answered Dec 29 '18 at 21:37
Jeevi BJeevi B
361
361
add a comment |
add a comment |
Since I came to web from pure Java backends - Exceptions in JAVA are bad (generally speaking), why follow same bad practices (using errors as info) when using HTTP?
After some more reading I can say - don't unless its actually an error, e.g:
- 404 if client tries to access entity with ID that is not there.
- 500 when server ACTUALLY can't handle something (actual Exception).
Here is Angular part for my Result<?>
system.
Interceptor:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Globals } from '../globals.service';
@Injectable()
export class PopupInterceptor implements HttpInterceptor {
constructor(private globals: Globals) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.startsWith(this.globals.apiHost)) {
return next.handle(req)
.pipe(
map(e => {
if (e instanceof HttpResponse) {
const response = <Response>e.body;
// use response to do whatever - I am injecting popup service and pushing response.message there for display in component.
return e.clone({ body: response.result });
}
})
);
}
return next.handle(req);
}
}
interface Response {
type: string;
message: string;
result: any;
}
globals.service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class Globals {
apiHost = '/api/';
}
app.module:
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: PopupInterceptor, multi: true }
],
add a comment |
Since I came to web from pure Java backends - Exceptions in JAVA are bad (generally speaking), why follow same bad practices (using errors as info) when using HTTP?
After some more reading I can say - don't unless its actually an error, e.g:
- 404 if client tries to access entity with ID that is not there.
- 500 when server ACTUALLY can't handle something (actual Exception).
Here is Angular part for my Result<?>
system.
Interceptor:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Globals } from '../globals.service';
@Injectable()
export class PopupInterceptor implements HttpInterceptor {
constructor(private globals: Globals) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.startsWith(this.globals.apiHost)) {
return next.handle(req)
.pipe(
map(e => {
if (e instanceof HttpResponse) {
const response = <Response>e.body;
// use response to do whatever - I am injecting popup service and pushing response.message there for display in component.
return e.clone({ body: response.result });
}
})
);
}
return next.handle(req);
}
}
interface Response {
type: string;
message: string;
result: any;
}
globals.service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class Globals {
apiHost = '/api/';
}
app.module:
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: PopupInterceptor, multi: true }
],
add a comment |
Since I came to web from pure Java backends - Exceptions in JAVA are bad (generally speaking), why follow same bad practices (using errors as info) when using HTTP?
After some more reading I can say - don't unless its actually an error, e.g:
- 404 if client tries to access entity with ID that is not there.
- 500 when server ACTUALLY can't handle something (actual Exception).
Here is Angular part for my Result<?>
system.
Interceptor:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Globals } from '../globals.service';
@Injectable()
export class PopupInterceptor implements HttpInterceptor {
constructor(private globals: Globals) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.startsWith(this.globals.apiHost)) {
return next.handle(req)
.pipe(
map(e => {
if (e instanceof HttpResponse) {
const response = <Response>e.body;
// use response to do whatever - I am injecting popup service and pushing response.message there for display in component.
return e.clone({ body: response.result });
}
})
);
}
return next.handle(req);
}
}
interface Response {
type: string;
message: string;
result: any;
}
globals.service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class Globals {
apiHost = '/api/';
}
app.module:
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: PopupInterceptor, multi: true }
],
Since I came to web from pure Java backends - Exceptions in JAVA are bad (generally speaking), why follow same bad practices (using errors as info) when using HTTP?
After some more reading I can say - don't unless its actually an error, e.g:
- 404 if client tries to access entity with ID that is not there.
- 500 when server ACTUALLY can't handle something (actual Exception).
Here is Angular part for my Result<?>
system.
Interceptor:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Globals } from '../globals.service';
@Injectable()
export class PopupInterceptor implements HttpInterceptor {
constructor(private globals: Globals) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.url.startsWith(this.globals.apiHost)) {
return next.handle(req)
.pipe(
map(e => {
if (e instanceof HttpResponse) {
const response = <Response>e.body;
// use response to do whatever - I am injecting popup service and pushing response.message there for display in component.
return e.clone({ body: response.result });
}
})
);
}
return next.handle(req);
}
}
interface Response {
type: string;
message: string;
result: any;
}
globals.service:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class Globals {
apiHost = '/api/';
}
app.module:
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: PopupInterceptor, multi: true }
],
answered Dec 30 '18 at 3:43
ErnioErnio
397213
397213
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53972370%2fis-using-500-response-code-for-normal-app-flow-viable-designing-popup-messages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
everything you have described is exactly how everyone does it and yet no one feels shame about it. And 500 is ok for that
– NEGR KITAEC
Dec 29 '18 at 21:43