errors

errors


Node.js

const err1 = new Error('some error')

console.log(err1)

class FooError extends Error{
  constructor(message) {
    super(message)
    this.name = 'FooError'
    this.message = message
  }

  toString() {
    return this.message
  }
}

const err2 = new FooError('my custom error')

console.log(err2)

Output

Error: some error
{ FooError: my custom error }

Go

package main

import (
    "errors"
    "fmt"
)

type FooError struct {
    s string
}

func (f *FooError) Error() string {
    return f.s
}

func NewFooError(s string) error {
    return &FooError{s}
}

func main() {
    err1 := errors.New("some error")
    fmt.Println(err1)

    err2 := NewFooError("my custom error")
    fmt.Println(err2)
}

Output

some error
my custom error