如何让指定响应码原生触发错误?request-promise-native使用疑问
I see exactly what you're trying to do—you want the catch block to trigger not just for non-2xx status codes, but for any status code that isn't exactly 200 (like 201, 204, etc.). The default behavior of request-promise-native only throws errors for non-2xx responses, but we can fix that with a few tweaks, or switch to a more modern tool for better long-term support.
Option 1: Custom Validation with request-promise-native
Since you're already using resolveWithFullResponse: true, you have two clean ways to enforce your status code rule:
Manual Check in the Try Block
This is straightforward and easy to read—just add a check after receiving the response:
const request = require('request-promise-native'); (async () => { try { const response = await request({ uri: 'http://httpbin.org/status/201', resolveWithFullResponse: true, simple: true, // Default setting: ensures non-2xx codes throw first }); // Custom validation: only proceed if status is exactly 200 if (response.statusCode !== 200) { throw new Error(`Unexpected status code: ${response.statusCode}`); } console.log(response.statusCode); // Only runs if status is 200 } catch (error) { console.error(error); // Catches non-2xx AND 2xx non-200 codes } })();
Using the transform Hook
For a more encapsulated approach, use the transform function to validate the status before the promise resolves:
const request = require('request-promise-native'); (async () => { try { const response = await request({ uri: 'http://httpbin.org/status/201', resolveWithFullResponse: true, simple: true, transform: (body, response) => { if (response.statusCode !== 200) { throw new Error(`Status code ${response.statusCode} is not 200`); } return response; // Only returns if validation passes } }); console.log(response.statusCode); } catch (error) { console.error(error); } })();
Option 2: Switch to a Modern HTTP Client (Recommended)
Note that request-promise-native relies on the request library, which is no longer maintained. For better long-term support, consider switching to tools that let you define custom success rules natively:
Axios
Axios has a built-in validateStatus option that lets you explicitly define which status codes count as successful:
const axios = require('axios'); (async () => { try { const response = await axios({ url: 'http://httpbin.org/status/201', validateStatus: (status) => status === 200, // Only 200 counts as success }); console.log(response.status); } catch (error) { console.error(error); // Triggers for 201, 404, 500, etc. } })();
node-fetch
node-fetch (the Node.js implementation of the browser Fetch API) is lightweight and standards-compliant—just add a manual check for the exact status code:
const fetch = require('node-fetch'); (async () => { try { const response = await fetch('http://httpbin.org/status/201'); // Reject if status isn't exactly 200 if (response.status !== 200) { throw new Error(`Unexpected status code: ${response.status}`); } console.log(response.status); } catch (error) { console.error(error); } })();
内容的提问来源于stack exchange,提问作者Sergei Ivanov

