Recent Posts

Featured image for How to Get Yesterday's Date in Bash on Mac and Ubuntu

How to Get Yesterday's Date in Bash on Mac and Ubuntu

In Bash scripting, there are various ways to retrieve yesterday’s date on both Mac and Ubuntu systems. Below, we’ll demonstrate two different methods for each operating system.

Mac:

Method 1: Using date with -v option

bash
1
2
yesterday=$(date -v-1d +%F)
echo "Yesterday's date on Mac: $yesterday"

In this method, we use the -v option with the date command to subtract 1 day from the current date and format it as %F, which gives the date in YYYY-MM-DD format.

Featured image for Shell Bash Export variable

Shell Bash Export variable

It looks like you’re trying to use the eval command in a Bash script to export variables loaded from a dotenv file using the shdotenv tool. This is a common practice for setting environment variables from a configuration file. Here’s an explanation of what this code does:

  1. shdotenv is likely a command or script that reads a dotenv file (usually named .env) and sets environment variables based on the key-value pairs defined in that file. Dotenv files are commonly used to store configuration variables for applications.

  2. $(shdotenv) is a subshell command substitution. It runs the shdotenv command and captures its standard output.

  3. eval is a Bash built-in command that evaluates and executes the command passed to it as a string. In this case, it’s used to execute the output of shdotenv as if it were a series of Bash commands.

Featured image for Golang Interface{} and Type Assertions

Golang Interface{} and Type Assertions

In Go (Golang), the interface{} type is an empty interface that can hold values of any type. It is often used when you need to work with values of unknown or varied types. Type assertions allow you to extract and work with the underlying concrete type of a value stored in an interface{}. Here, we’ll explore how to use interface{} and type assertions in Go.

Storing Different Types in an interface{}

You can store values of different types in an interface{}. Here’s an example:

Featured image for REST API Status Code Example

REST API Status Code Example

Introduction

HTTP status codes are essential in REST APIs as they indicate the outcome of client requests. These standardized codes help clients understand whether their request was successful, requires further action, or encountered an error. Below is an overview of key HTTP status codes categorized by their respective classes.

Featured image for REST API Error Response Example

REST API Error Response Example

REST APIs should provide clear and structured error responses to help clients understand and resolve issues effectively. Below are examples of different types of error responses that an API might return.


Single Error Responses

Incorrect Username or Password

json
1
2
3
4
5
6
7
{
    "type": "/errors/incorrect-user-pass",
    "title": "Incorrect username or password.",
    "status": 401,
    "detail": "Authentication failed due to incorrect username or password.",
    "instance": "/login/log/abc123"
}

Validation Errors

Title Must Be Defined

json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    "data": null,
    "error": {
        "status": 400,
        "name": "ValidationError",
        "message": "title must be defined.",
        "details": {
            "errors": [
                {
                    "path": ["title"],
                    "message": "title must be defined.",
                    "name": "ValidationError"
                }
            ]
        }
    }
}

Name Must Be At Most 50 Characters

json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    "data": null,
    "error": {
        "status": 400,
        "name": "ValidationError",
        "message": "name must be at most 50 characters",
        "details": {
            "errors": [
                {
                    "path": ["name"],
                    "message": "name must be at most 50 characters",
                    "name": "ValidationError"
                }
            ]
        }
    }
}

This Attribute Must Be Unique

json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
    "data": null,
    "error": {
        "status": 400,
        "name": "ValidationError",
        "message": "This attribute must be unique",
        "details": {
            "errors": [
                {
                    "path": ["name"],
                    "message": "This attribute must be unique",
                    "name": "ValidationError"
                }
            ]
        }
    }
}

Authentication Errors

No Authorization Provided

json
1
2
3
4
5
6
7
8
9
{
    "data": null,
    "error": {
        "status": 403,
        "name": "ForbiddenError",
        "message": "Forbidden",
        "details": {}
    }
}

Invalid Authentication

json
1
2
3
4
5
6
7
8
9
{
    "data": null,
    "error": {
        "status": 401,
        "name": "UnauthorizedError",
        "message": "Missing or invalid credentials",
        "details": {}
    }
}

Multiple Errors

Name and Title Must Be Defined

json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
    "data": null,
    "error": {
        "status": 400,
        "name": "ValidationError",
        "message": "2 errors occurred",
        "details": {
            "errors": [
                {
                    "path": ["name"],
                    "message": "name must be defined.",
                    "name": "ValidationError"
                },
                {
                    "path": ["title"],
                    "message": "title must be defined.",
                    "name": "ValidationError"
                }
            ]
        }
    }
}

Field-Specific Validation Errors

Field Too Short

json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
  "placement": "field",
  "title": "value too short",
  "detail": "field username must be at least 4 symbols",
  "location": "username",
  "field": "username",
  "code": "validation.min",
  "expression": "min",
  "argument": "4",
  "traceid": "74681b27-b1ea-454d-9847-d27059e19119",
  "stacktraces": [
 {
   "file": "model/response.go",
   "function": "model.(*ErrorMessage).LogStacktraceWithErr",
   "linenumber": 22,
   "realerror": null
 },
 {
   "file": "helpers/validator.go",
   "function": "helpers.Validator",
   "linenumber": 58,
   "realerror": null
 },
 {
   "file": "handlers/registration.go",
   "function": "handlers.RegistrationHandler",
   "linenumber": 61,
   "realerror": null
 }
  ]
}

Password and Confirm Password Must Match

json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
  "placement": "field",
  "title": "must be equal",
  "detail": "field password is not equal to field confirmpassword",
  "location": "password",
  "field": "password",
  "code": "validation.equal",
  "expression": "equal",
  "argument": "confirmpassword",
  "traceid": "e017ecb2-d72f-4f79-889f-6c42126970a8",
  "stacktraces": [
 {
   "file": "model/response.go",
   "function": "model.(*ErrorMessage).LogStacktraceWithErr",
   "linenumber": 22,
   "realerror": null
 },
 {
   "file": "helpers/validator.go",
   "function": "helpers.Validator",
   "linenumber": 58,
   "realerror": null
 },
 {
   "file": "handlers/registration.go",
   "function": "handlers.RegistrationHandler",
   "linenumber": 61,
   "realerror": null
 }
  ]
}

Incorrect Email Format

json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
  "placement": "field",
  "title": "incorrect email format",
  "detail": "field email has incorrect value",
  "location": "email",
  "field": "email",
  "code": "validation.email",
  "expression": null,
  "argument": null,
  "traceid": "cecda6b8-7ce8-4054-8c06-9382320afd78",
  "stacktraces": [
 {
   "file": "model/response.go",
   "function": "model.(*ErrorMessage).LogStacktraceWithErr",
   "linenumber": 22,
   "realerror": null
 },
 {
   "file": "helpers/validator.go",
   "function": "helpers.Validator",
   "linenumber": 58,
   "realerror": null
 },
 {
   "file": "handlers/registration.go",
   "function": "handlers.RegistrationHandler",
   "linenumber": 61,
   "realerror": null
 }
  ]
}

These error responses provide a consistent structure, making it easier for clients to handle errors programmatically and display meaningful messages to users.