How can I access the data from new Error(data) in catch method?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Here is my promise:
.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});
The console.log that is executed just prior to throw Error(response); shows this nice information:
will throw error
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}
But the console.log("catching error", error) in the catch method shows
catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:
catching error {}
- How can I access the data I passed to the new error?
- How should I fix this?
javascript promise
add a comment |
Here is my promise:
.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});
The console.log that is executed just prior to throw Error(response); shows this nice information:
will throw error
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}
But the console.log("catching error", error) in the catch method shows
catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:
catching error {}
- How can I access the data I passed to the new error?
- How should I fix this?
javascript promise
Try logging theerror.messageintead
– Bergi
Nov 16 '18 at 15:23
add a comment |
Here is my promise:
.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});
The console.log that is executed just prior to throw Error(response); shows this nice information:
will throw error
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}
But the console.log("catching error", error) in the catch method shows
catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:
catching error {}
- How can I access the data I passed to the new error?
- How should I fix this?
javascript promise
Here is my promise:
.then(function(response) {
if (response.type === 'error') {
console.log("will throw error", response);
throw Error(response);
}
return extractResponseCallback(response);
})
.catch(function(error) {
console.log("catching error", error);
return error;
});
The console.log that is executed just prior to throw Error(response); shows this nice information:
will throw error
{
cmd: 'has_active_project',
errorcode: 'Invalid command',
type: 'error'
}
But the console.log("catching error", error) in the catch method shows
catching error Error: [object Object]
at /....js:18:19
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Furthermore, using console.log(JSON.stringify("catching error", error)) only shows:
catching error {}
- How can I access the data I passed to the new error?
- How should I fix this?
javascript promise
javascript promise
asked Nov 16 '18 at 15:10
user1283776user1283776
3,7132160117
3,7132160117
Try logging theerror.messageintead
– Bergi
Nov 16 '18 at 15:23
add a comment |
Try logging theerror.messageintead
– Bergi
Nov 16 '18 at 15:23
Try logging the
error.message intead– Bergi
Nov 16 '18 at 15:23
Try logging the
error.message intead– Bergi
Nov 16 '18 at 15:23
add a comment |
2 Answers
2
active
oldest
votes
The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:
throw new Error(JSON.stringify(response))
Then you can access that message in your catch handler with
error.message
or even
JSON.parse(error.message)
if you need to interact with the individual properties for some reason.
add a comment |
I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.
From docs:
Displays an interactive list of the properties of the specified
JavaScript object. The output is presented as a hierarchical listing
with disclosure triangles that let you see the contents of child
objects.
In other words, console.dir is the way to see all the properties of a
specified JavaScript object in console by which the developer can
easily get the properties of the object.
Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.
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%2f53340496%2fhow-can-i-access-the-data-from-new-errordata-in-catch-method%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
The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:
throw new Error(JSON.stringify(response))
Then you can access that message in your catch handler with
error.message
or even
JSON.parse(error.message)
if you need to interact with the individual properties for some reason.
add a comment |
The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:
throw new Error(JSON.stringify(response))
Then you can access that message in your catch handler with
error.message
or even
JSON.parse(error.message)
if you need to interact with the individual properties for some reason.
add a comment |
The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:
throw new Error(JSON.stringify(response))
Then you can access that message in your catch handler with
error.message
or even
JSON.parse(error.message)
if you need to interact with the individual properties for some reason.
The argument to the Error constructor is a string and results in the message property on the resulting error object. You could try this if you know your custom error will be an object:
throw new Error(JSON.stringify(response))
Then you can access that message in your catch handler with
error.message
or even
JSON.parse(error.message)
if you need to interact with the individual properties for some reason.
edited Nov 16 '18 at 17:02
answered Nov 16 '18 at 15:17
Ben StewardBen Steward
1,261516
1,261516
add a comment |
add a comment |
I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.
From docs:
Displays an interactive list of the properties of the specified
JavaScript object. The output is presented as a hierarchical listing
with disclosure triangles that let you see the contents of child
objects.
In other words, console.dir is the way to see all the properties of a
specified JavaScript object in console by which the developer can
easily get the properties of the object.
Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.
add a comment |
I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.
From docs:
Displays an interactive list of the properties of the specified
JavaScript object. The output is presented as a hierarchical listing
with disclosure triangles that let you see the contents of child
objects.
In other words, console.dir is the way to see all the properties of a
specified JavaScript object in console by which the developer can
easily get the properties of the object.
Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.
add a comment |
I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.
From docs:
Displays an interactive list of the properties of the specified
JavaScript object. The output is presented as a hierarchical listing
with disclosure triangles that let you see the contents of child
objects.
In other words, console.dir is the way to see all the properties of a
specified JavaScript object in console by which the developer can
easily get the properties of the object.
Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.
I believe you're searching for console.dir(), it shows all properties of the passed object, and you see the normal object with interactive properties and values instead of uninformative [object Object] output.
From docs:
Displays an interactive list of the properties of the specified
JavaScript object. The output is presented as a hierarchical listing
with disclosure triangles that let you see the contents of child
objects.
In other words, console.dir is the way to see all the properties of a
specified JavaScript object in console by which the developer can
easily get the properties of the object.
Follow this way if you really need to log all these info. Notice, that when you want to see a specific field of error object, you can just log like error.message, it's enough for most cases.
edited Nov 16 '18 at 15:47
answered Nov 16 '18 at 15:14
Commercial SuicideCommercial Suicide
10k113354
10k113354
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%2f53340496%2fhow-can-i-access-the-data-from-new-errordata-in-catch-method%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
Try logging the
error.messageintead– Bergi
Nov 16 '18 at 15:23