Access the request path variable from an error gson view?












1















I have a RESTful api build around a simple user (grails) domain class like this one:



class User{
Long id
static constraints = {
}
}


And I also have built a controller class for that user:



class UserController extends RestfulController {
static responseFormats = ['json', 'xml']

UserController() {
super(User)
}
}


Grails generates all the "regular crud endpoints" for me, which is nice but I want to modify the regular 404 error for a new one with the user id which can be found as a path variable (resource's id), for example:



www.url/user/3


If there is no user with id 3, I want this json error response:



{"errorMessage":"user 3 not found"}


To achieve this, I have modified the 404 gson view:



response.status 404



json {
message "User ${request.PATHVARIABLE} not found."
error 404
}


This is partially working because my custom json message is shown but I can't manage to access the path variable on the view, I know that I can access the request but I don't find a valid method that returns the path variable on that request (the PATHVARIABLE method shown on the example below it's just an example, not a real method).



Can I access this id from the gson error view somehow?



UPDATE:
in my view, if I try to get the request uri:



json {
uri request.uri
params params
message "User ${params.id} not found."
error 404
}


When I try to get a user that does not exist with /user/123, this is my response:



{
"uri": "/error",
"params": {
"parameterMap": {},
"empty": true
},
"message": "User null not found.",
"error": 404
}


why the request uri is /error instead of my original request uri?










share|improve this question

























  • An option would be to do request.uri.split('/').last() - but it makes a lot of assumptions about your uri (which may be ok in your case)

    – erichelgeson
    Nov 15 '18 at 15:54











  • I already have though that before but I don't want to parse the uri that way, In spring there are ways to get the path variable, can't I do the same in grails?

    – Blazerg
    Nov 15 '18 at 16:10











  • In spring what object are you getting it from?

    – erichelgeson
    Nov 15 '18 at 16:11











  • Just looked at the api of HttpView and it has the parsed params object - so posted an answer. Just tested on Grails 3.3.8 & Views 1.2.8

    – erichelgeson
    Nov 15 '18 at 16:19
















1















I have a RESTful api build around a simple user (grails) domain class like this one:



class User{
Long id
static constraints = {
}
}


And I also have built a controller class for that user:



class UserController extends RestfulController {
static responseFormats = ['json', 'xml']

UserController() {
super(User)
}
}


Grails generates all the "regular crud endpoints" for me, which is nice but I want to modify the regular 404 error for a new one with the user id which can be found as a path variable (resource's id), for example:



www.url/user/3


If there is no user with id 3, I want this json error response:



{"errorMessage":"user 3 not found"}


To achieve this, I have modified the 404 gson view:



response.status 404



json {
message "User ${request.PATHVARIABLE} not found."
error 404
}


This is partially working because my custom json message is shown but I can't manage to access the path variable on the view, I know that I can access the request but I don't find a valid method that returns the path variable on that request (the PATHVARIABLE method shown on the example below it's just an example, not a real method).



Can I access this id from the gson error view somehow?



UPDATE:
in my view, if I try to get the request uri:



json {
uri request.uri
params params
message "User ${params.id} not found."
error 404
}


When I try to get a user that does not exist with /user/123, this is my response:



{
"uri": "/error",
"params": {
"parameterMap": {},
"empty": true
},
"message": "User null not found.",
"error": 404
}


why the request uri is /error instead of my original request uri?










share|improve this question

























  • An option would be to do request.uri.split('/').last() - but it makes a lot of assumptions about your uri (which may be ok in your case)

    – erichelgeson
    Nov 15 '18 at 15:54











  • I already have though that before but I don't want to parse the uri that way, In spring there are ways to get the path variable, can't I do the same in grails?

    – Blazerg
    Nov 15 '18 at 16:10











  • In spring what object are you getting it from?

    – erichelgeson
    Nov 15 '18 at 16:11











  • Just looked at the api of HttpView and it has the parsed params object - so posted an answer. Just tested on Grails 3.3.8 & Views 1.2.8

    – erichelgeson
    Nov 15 '18 at 16:19














1












1








1








I have a RESTful api build around a simple user (grails) domain class like this one:



class User{
Long id
static constraints = {
}
}


And I also have built a controller class for that user:



class UserController extends RestfulController {
static responseFormats = ['json', 'xml']

UserController() {
super(User)
}
}


Grails generates all the "regular crud endpoints" for me, which is nice but I want to modify the regular 404 error for a new one with the user id which can be found as a path variable (resource's id), for example:



www.url/user/3


If there is no user with id 3, I want this json error response:



{"errorMessage":"user 3 not found"}


To achieve this, I have modified the 404 gson view:



response.status 404



json {
message "User ${request.PATHVARIABLE} not found."
error 404
}


This is partially working because my custom json message is shown but I can't manage to access the path variable on the view, I know that I can access the request but I don't find a valid method that returns the path variable on that request (the PATHVARIABLE method shown on the example below it's just an example, not a real method).



Can I access this id from the gson error view somehow?



UPDATE:
in my view, if I try to get the request uri:



json {
uri request.uri
params params
message "User ${params.id} not found."
error 404
}


When I try to get a user that does not exist with /user/123, this is my response:



{
"uri": "/error",
"params": {
"parameterMap": {},
"empty": true
},
"message": "User null not found.",
"error": 404
}


why the request uri is /error instead of my original request uri?










share|improve this question
















I have a RESTful api build around a simple user (grails) domain class like this one:



class User{
Long id
static constraints = {
}
}


And I also have built a controller class for that user:



class UserController extends RestfulController {
static responseFormats = ['json', 'xml']

UserController() {
super(User)
}
}


Grails generates all the "regular crud endpoints" for me, which is nice but I want to modify the regular 404 error for a new one with the user id which can be found as a path variable (resource's id), for example:



www.url/user/3


If there is no user with id 3, I want this json error response:



{"errorMessage":"user 3 not found"}


To achieve this, I have modified the 404 gson view:



response.status 404



json {
message "User ${request.PATHVARIABLE} not found."
error 404
}


This is partially working because my custom json message is shown but I can't manage to access the path variable on the view, I know that I can access the request but I don't find a valid method that returns the path variable on that request (the PATHVARIABLE method shown on the example below it's just an example, not a real method).



Can I access this id from the gson error view somehow?



UPDATE:
in my view, if I try to get the request uri:



json {
uri request.uri
params params
message "User ${params.id} not found."
error 404
}


When I try to get a user that does not exist with /user/123, this is my response:



{
"uri": "/error",
"params": {
"parameterMap": {},
"empty": true
},
"message": "User null not found.",
"error": 404
}


why the request uri is /error instead of my original request uri?







rest grails groovy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:12







Blazerg

















asked Nov 15 '18 at 14:37









BlazergBlazerg

82215




82215













  • An option would be to do request.uri.split('/').last() - but it makes a lot of assumptions about your uri (which may be ok in your case)

    – erichelgeson
    Nov 15 '18 at 15:54











  • I already have though that before but I don't want to parse the uri that way, In spring there are ways to get the path variable, can't I do the same in grails?

    – Blazerg
    Nov 15 '18 at 16:10











  • In spring what object are you getting it from?

    – erichelgeson
    Nov 15 '18 at 16:11











  • Just looked at the api of HttpView and it has the parsed params object - so posted an answer. Just tested on Grails 3.3.8 & Views 1.2.8

    – erichelgeson
    Nov 15 '18 at 16:19



















  • An option would be to do request.uri.split('/').last() - but it makes a lot of assumptions about your uri (which may be ok in your case)

    – erichelgeson
    Nov 15 '18 at 15:54











  • I already have though that before but I don't want to parse the uri that way, In spring there are ways to get the path variable, can't I do the same in grails?

    – Blazerg
    Nov 15 '18 at 16:10











  • In spring what object are you getting it from?

    – erichelgeson
    Nov 15 '18 at 16:11











  • Just looked at the api of HttpView and it has the parsed params object - so posted an answer. Just tested on Grails 3.3.8 & Views 1.2.8

    – erichelgeson
    Nov 15 '18 at 16:19

















An option would be to do request.uri.split('/').last() - but it makes a lot of assumptions about your uri (which may be ok in your case)

– erichelgeson
Nov 15 '18 at 15:54





An option would be to do request.uri.split('/').last() - but it makes a lot of assumptions about your uri (which may be ok in your case)

– erichelgeson
Nov 15 '18 at 15:54













I already have though that before but I don't want to parse the uri that way, In spring there are ways to get the path variable, can't I do the same in grails?

– Blazerg
Nov 15 '18 at 16:10





I already have though that before but I don't want to parse the uri that way, In spring there are ways to get the path variable, can't I do the same in grails?

– Blazerg
Nov 15 '18 at 16:10













In spring what object are you getting it from?

– erichelgeson
Nov 15 '18 at 16:11





In spring what object are you getting it from?

– erichelgeson
Nov 15 '18 at 16:11













Just looked at the api of HttpView and it has the parsed params object - so posted an answer. Just tested on Grails 3.3.8 & Views 1.2.8

– erichelgeson
Nov 15 '18 at 16:19





Just looked at the api of HttpView and it has the parsed params object - so posted an answer. Just tested on Grails 3.3.8 & Views 1.2.8

– erichelgeson
Nov 15 '18 at 16:19












1 Answer
1






active

oldest

votes


















1














The json view will have a params object so you can do:



json {
if (params.id) {
message "User ${params.id} not found."
} else {
message "Not found."
}
error 404
}





share|improve this answer
























  • is not working for me, when I do a GET call to user/1 the message is: "User null not found"

    – Blazerg
    Nov 15 '18 at 17:16











  • Try json { params params } to get a full dump of your params to debug.

    – erichelgeson
    Nov 15 '18 at 17:18











  • there ir nothing: { "params": { "parameterMap": {}, "empty": true }, "message": "User null not found.", "error": 404 }

    – Blazerg
    Nov 15 '18 at 17:23











  • I have updated the question in case, in case it helps you to see what's happening

    – Blazerg
    Nov 15 '18 at 18:13











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321804%2faccess-the-request-path-variable-from-an-error-gson-view%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The json view will have a params object so you can do:



json {
if (params.id) {
message "User ${params.id} not found."
} else {
message "Not found."
}
error 404
}





share|improve this answer
























  • is not working for me, when I do a GET call to user/1 the message is: "User null not found"

    – Blazerg
    Nov 15 '18 at 17:16











  • Try json { params params } to get a full dump of your params to debug.

    – erichelgeson
    Nov 15 '18 at 17:18











  • there ir nothing: { "params": { "parameterMap": {}, "empty": true }, "message": "User null not found.", "error": 404 }

    – Blazerg
    Nov 15 '18 at 17:23











  • I have updated the question in case, in case it helps you to see what's happening

    – Blazerg
    Nov 15 '18 at 18:13
















1














The json view will have a params object so you can do:



json {
if (params.id) {
message "User ${params.id} not found."
} else {
message "Not found."
}
error 404
}





share|improve this answer
























  • is not working for me, when I do a GET call to user/1 the message is: "User null not found"

    – Blazerg
    Nov 15 '18 at 17:16











  • Try json { params params } to get a full dump of your params to debug.

    – erichelgeson
    Nov 15 '18 at 17:18











  • there ir nothing: { "params": { "parameterMap": {}, "empty": true }, "message": "User null not found.", "error": 404 }

    – Blazerg
    Nov 15 '18 at 17:23











  • I have updated the question in case, in case it helps you to see what's happening

    – Blazerg
    Nov 15 '18 at 18:13














1












1








1







The json view will have a params object so you can do:



json {
if (params.id) {
message "User ${params.id} not found."
} else {
message "Not found."
}
error 404
}





share|improve this answer













The json view will have a params object so you can do:



json {
if (params.id) {
message "User ${params.id} not found."
} else {
message "Not found."
}
error 404
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 16:17









erichelgesonerichelgeson

1,126916




1,126916













  • is not working for me, when I do a GET call to user/1 the message is: "User null not found"

    – Blazerg
    Nov 15 '18 at 17:16











  • Try json { params params } to get a full dump of your params to debug.

    – erichelgeson
    Nov 15 '18 at 17:18











  • there ir nothing: { "params": { "parameterMap": {}, "empty": true }, "message": "User null not found.", "error": 404 }

    – Blazerg
    Nov 15 '18 at 17:23











  • I have updated the question in case, in case it helps you to see what's happening

    – Blazerg
    Nov 15 '18 at 18:13



















  • is not working for me, when I do a GET call to user/1 the message is: "User null not found"

    – Blazerg
    Nov 15 '18 at 17:16











  • Try json { params params } to get a full dump of your params to debug.

    – erichelgeson
    Nov 15 '18 at 17:18











  • there ir nothing: { "params": { "parameterMap": {}, "empty": true }, "message": "User null not found.", "error": 404 }

    – Blazerg
    Nov 15 '18 at 17:23











  • I have updated the question in case, in case it helps you to see what's happening

    – Blazerg
    Nov 15 '18 at 18:13

















is not working for me, when I do a GET call to user/1 the message is: "User null not found"

– Blazerg
Nov 15 '18 at 17:16





is not working for me, when I do a GET call to user/1 the message is: "User null not found"

– Blazerg
Nov 15 '18 at 17:16













Try json { params params } to get a full dump of your params to debug.

– erichelgeson
Nov 15 '18 at 17:18





Try json { params params } to get a full dump of your params to debug.

– erichelgeson
Nov 15 '18 at 17:18













there ir nothing: { "params": { "parameterMap": {}, "empty": true }, "message": "User null not found.", "error": 404 }

– Blazerg
Nov 15 '18 at 17:23





there ir nothing: { "params": { "parameterMap": {}, "empty": true }, "message": "User null not found.", "error": 404 }

– Blazerg
Nov 15 '18 at 17:23













I have updated the question in case, in case it helps you to see what's happening

– Blazerg
Nov 15 '18 at 18:13





I have updated the question in case, in case it helps you to see what's happening

– Blazerg
Nov 15 '18 at 18:13




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321804%2faccess-the-request-path-variable-from-an-error-gson-view%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues