title: How to throw an error in nextjs (server) if the method is GET and supported but no parameter is passed?
body: <pre><code>export default function handler(req, res {
    const {
      method,
      query: { pid },
    } = req;
  
    if (method === 'GET') {
      if (pid) {
        res.statusCode = 200;
        res.end(`Post: ${pid}`);
      } else {
        try {
          const error = 'No post id specified';
          throw new Error(error);
        } catch (err) {
          res.statusCode = 400;
          res.end(`error: ${err}`);
        }
      }
    } else {
      const error = `unsupported method ${method}`;
      try {
        throw new Error(error);
      } catch (err) {
        res.statusCode = 400;
        res.end(`error: ${err}`);
      }
    }
  }
  </code></pre>
  <blockquote>
  <p>If the route <code>/posts/</code> is called (without specifying <code>pid</code>), the above will return the 404 page's HTML but not the intended error &quot;No post id specified&quot;</p>
  </blockquote>
  <p>The unsupported route if-branch works correctly instead.</p>
  <p><strong>How to obtain the above-explained behavior</strong>?</p>