Dimas Maulana

Dimas Maulana

Developer

Welcome to my website! I am a developer with a current focus on React and Go. My experience encompasses both front-end and back-end development, enabling me to design and develop seamless and efficient applications.

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:

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.

REST API Success Response Example

Overview

This document provides an example of a successful response from a REST API when creating a new resource. The response follows a structured format, including essential details such as the resource ID, attributes, timestamps, and metadata.

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

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

 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

 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

 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

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

Invalid Authentication

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

 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

 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

 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

 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.

Golang Go Get Postgres Error

You are dealing with error handling in Go when working with PostgreSQL using the pq package. The code you provided demonstrates two different ways to handle and extract error information from a pq.Error type.

Let’s break down both of these code snippets:

  1. Using Type Assertion:
1
2
pqErr := err.(*pq.Error)
log.Println(pqErr.Code)

In this code, you are using a type assertion to check if the err is of type *pq.Error, and if it is, you extract the Code field from the pq.Error struct and log it. This approach assumes that err is a pq.Error type, and if it’s not, it will result in a runtime panic. So, it’s essential to be sure that err is indeed of type *pq.Error before using this approach.

Bash Shell Ignore Error on Particular Command

In Bash scripting, you can use the || true construct to ignore errors for a particular command or script. This is a common technique used to ensure that a script continues executing even if a specific command fails. Here’s how it works:

1
particular_script || true

In this example, particular_script is the command or script that you want to run, and || true is added at the end of the command. The || operator is used for conditional execution. It means that if particular_script fails (returns a non-zero exit status), the true command will always execute, effectively ignoring the error and allowing the script to continue running.

0%