Friday, 1 December 2023

What is the actual output of console.log(error) and how do I get the exact same output as a string?

In a Node.js project using Typescript and targeting ES2020, I'm using a custom Error class like this:

class InvalidParamsError extends Error {
}

try {
  throw new InvalidParamsError();
} catch (error) {
  if (error instanceof Error) {
    console.log(error);
    console.log(error.message); // Different output
    console.log(error.stack); // Different output
    console.log(error.toString()); // Different output
  }
}

The output I get in the console is:

InvalidParamsError
    at REPL11:2:13
    at ...

Error
    at REPL11:2:13
    at ...
Error
undefined

How can I get this very same output as a string without having to print it to the console?

error.message, error.stack and error.toString() all return different strings, and none of them show InvalidParamsError as part of the output, showing a generic Error instead. I know I could fix this manually setting the name of the InvalidParamsError in the constructor, but even then I'm unable to get exactly the same output as console.log(error) as a string.

I tried looking at Node.js' console source code but couldn't find any answer.

I'm targeting ES2020 so I don't think this is related with previous Typescript issues when extending built-ins like Error.



from What is the actual output of console.log(error) and how do I get the exact same output as a string?

GCP python script on VM cloud logging - No logs appearing

I am new to GCP / cloud services in general. If I have a simple python script below, how can I get it to show up in the google cloud logging explorer without modifying the python code? Is there something I need to enable on the gcp side?

import logging
import time
import sys

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

logging.getLogger().setLevel(logging.INFO)

while True:
    print('****** print ******')
    time.sleep(3)
    logging.info('****** info ******')
    time.sleep(3)
    logging.critical('****** critical ******')
    time.sleep(3)
    sys.stdout.flush()

Here is what I've tried:

  1. Install the OPS agent when creating the VM
  2. All permissions in IAM are granted for logging
  3. Here are some of the commands I ran:
  • sudo journalctl -u google-fluentd -f
  • sudo journalctl -u google-fluentd -xe
  • sudo systemctl restart google-fluentd
  • sudo systemctl status google-fluentd

In cloud explorer I do see logs for when the fluentd service is restarted and other sys-logs like the one below, but I do not see any logs that are appearing in stdout (e.g. ****** print ******.

DEFAULT 2023-11-21T18:55:34Z [resource.labels.instanceId: test-service] Nov 21 18:55:34 debian systemd[1]: Starting GCE Workload Certificate refresh...
DEFAULT 2023-11-21T18:55:34Z [resource.labels.instanceId: test-service] Nov 21 18:55:34 debian gce_workload_cert_refresh[7493]: 2023/11/21 18:55:34: Done
DEFAULT 2023-11-21T18:55:34Z [resource.labels.instanceId: test-service] Nov 21 18:55:34 debian systemd[1]: gce-workload-cert-refresh.service: Succeeded.
DEFAULT 2023-11-21T18:55:34Z [resource.labels.instanceId: test-service] Nov 21 18:55:34 debian systemd[1]: Finished GCE Workload Certificate refresh.
DEFAULT 2023-11-21T18:55:38Z [resource.labels.instanceId: test-service] Nov 21 18:55:38 debian systemd[1]: Started Session 3 of user temp-user.
DEFAULT 2023-11-21T18:55:38Z [resource.labels.instanceId: test-service] Nov 21 18:55:38 debian dhclient[454]: XMT: Solicit on ens4, interval 122430ms.
DEFAULT 2023-11-21T18:57:41Z [resource.labels.instanceId: test-service] Nov 21 18:57:41 debian dhclient[454]: XMT: Solicit on ens4, interval 115540ms.
DEFAULT 2023-11-21T18:59:36Z [resource.labels.instanceId: test-service] Nov 21 18:59:36 debian dhclient[454]: XMT: Solicit on ens4, interval 117740ms.

Of course I am able to do something this:

import google.cloud.logging
import logging

client = google.cloud.logging.Client()
client.setup_logging()
logging.info('TEST')

The problem with the above is that I'm importing packages / SDKs where there are built-in logging / printing from the python standard library. I'm sure google doesn't think we're going to go in all of these huge packages and setup a google logging client. I basically just need everything that's sent from stdout to be also sent to google cloud logging.



from GCP python script on VM cloud logging - No logs appearing

React Router Not Working when running npm build and serving, but is when running npm start

Edit:

After testing through running 'npm run build' and then serving that locally with 'serve -s build' I realised the Nav Links from react-router don't work, but when using 'npm start' the Nav Links work fine. This means it was not an issue with Vercel or any kind of production configuration. But I can't figure out why this difference is - my app only contains a .env file with

REACT_APP_API_URL=https://ift.tt/HT40EaB

#REACT_APP_API_URL=http://localhost:8080

and no other environment variables are used on the frontend that could be affected by dev vs production. I've also tried changing my "homepage": ".", to a dot, and just "". The thing is, the routing works, since I click on it -> /about nothing happens, but when I refresh it works and it brings up the AboutPage component.

I also thought it could be service workers, but that lead to nothing seeing as there is no ServiceWorkers.js file or anything similar in the build directory, or in the service workers tab in applications tab of console.

I've deployed a React application on Vercel, and I'm encountering an issue where <Link> components from react-router-dom are not functioning as expected. When I click on these links, the URL changes, but the page content does not update. This issue is not present in my local development environment, where navigation works correctly.

The issue occurs only in the production build on Vercel and when I click on a <Link> it changes the URL but does not navigate to the new page, and no network requests or console errors appear when I click on the links.

  1. Navbar Component (using <LinkContainer> from react-router-bootstrap):
import React from "react";
import { LinkContainer } from 'react-router-bootstrap';
import {Navbar, Nav, Image} from 'react-bootstrap';
import logo from '../assets/logo.svg';

import '../css/Navbar.css';

const LPNavBar = () => {
    return (
        <Navbar expand="lg" style=>
            <LinkContainer to="/home">
                <Navbar.Brand className="navbar-brand-margin custom-logo">
                    <Image
                        src={logo}
                        width="30"
                        height="30"
                        className="d-inline-block align-top"
                        alt="Logo"
                    />
                </Navbar.Brand>
            </LinkContainer>
            <Navbar.Toggle aria-controls="basic-navbar-nav" />
            <Navbar.Collapse id="basic-navbar-nav" className="justify-content-end">
                <Nav>
                    <LinkContainer to="/">
                        <Nav.Link className="nav-link nav-link-text">Home</Nav.Link>
                    </LinkContainer>
                    <LinkContainer to="/about">
                        <Nav.Link className="nav-link nav-link-text">About</Nav.Link>
                    </LinkContainer>
                    <LinkContainer to="/sign-up">
                        <Nav.Link className="nav-link nav-link-text">Register</Nav.Link>
                    </LinkContainer>
                    <LinkContainer to="/login">
                        <Nav.Link className="nav-link nav-link-margin nav-link-text">Login</Nav.Link>
                    </LinkContainer>
                </Nav>
            </Navbar.Collapse>
        </Navbar>
    );
};

export default LPNavBar;





AppRouter: 

    import React from 'react';
    import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
    
    import HomePage from './pages/HomePage';
    import UserProfilePage from './pages/UserProfilePage';
    import LandingPage from './pages/LandingPage';
    ... other imports
    import PaymentCancelledPage from "./pages/PaymentCancelledPage";
    import ContactPage from "./pages/ContactPage";
    
    const AppRouter = () => {
        return (
            <Router>
                <Routes>
                    <Route path="/" element={<LandingPage />} />
                    <Route path="/home" element={<HomePage />} />
                    <Route path="/profile/:userId" element={<UserProfilePage />} />
                    ...
                    <Route path="/payment-cancelled" element={<PaymentCancelledPage />} />
                    <Route path="/contact" element={<ContactPage />} />
                </Routes>
            </Router>
        );
    };
    
    export default AppRouter;

And the truly perplexing thing is that in my project's root directory on Github repo, in the branch connected to Vercel there is the vercel.json file 
that should resolve this problem: 


    .idea
    public
    src
    .env
    .env.production
    .gitignore
    README.md
    htmltos.txt
    package-lock.json
    package.json
    tsandcs.txt
    vercel.json

,     vercel.json:

      {
      "rewrites":  [
        {"source": "/(.*)", "destination": "/"}
      ]
    },

There is no console output, or network requests when clicking on any of the navlinks. The only thing that changes, is the URL correctly, but the state doesn't change/no redirect.  


The url could be / for the home page, and then I click About and the url becomes /about but nothing else changes. However, React Router works for all other parts, if I type /about it redirects to the /about route that is defined in the AppRouter.js file.




from React Router Not Working when running npm build and serving, but is when running npm start