mirror of
https://github.com/gin-gonic/gin.git
synced 2026-04-11 14:11:46 +08:00
fix: Correct typos, improve documentation clarity, and remove dead code (#4511)
* fix: correct typos and improve documentation clarity - Fix typo "Oupps" to "Oops" in recovery test panic messages - Fix confusing documentation in Bind() and ShouldBind() methods that incorrectly stated "JSON or XML as a JSON input" - Remove double period in StaticFileFS documentation comment - Remove unused ErrorTypeNu constant that had duplicate comment with ErrorTypeAny and was never used in the codebase * tech: Fix the pull request routing link
This commit is contained in:
parent
d7776de7d4
commit
8e07d37c63
@ -36,6 +36,6 @@ Please ensure your pull request meets the following requirements:
|
|||||||
- All tests pass in available continuous integration systems (e.g., GitHub Actions).
|
- All tests pass in available continuous integration systems (e.g., GitHub Actions).
|
||||||
- Add or modify tests to cover your code changes.
|
- Add or modify tests to cover your code changes.
|
||||||
- If your pull request introduces a new feature, document it in [`docs/doc.md`](docs/doc.md), not in the README.
|
- If your pull request introduces a new feature, document it in [`docs/doc.md`](docs/doc.md), not in the README.
|
||||||
- Follow the checklist in the [Pull Request Template](.github/PULL_REQUEST_TEMPLATE.md:1).
|
- Follow the checklist in the [Pull Request Template](.github/PULL_REQUEST_TEMPLATE.md).
|
||||||
|
|
||||||
Thank you for contributing!
|
Thank you for contributing!
|
||||||
|
|||||||
@ -751,8 +751,8 @@ func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string, perm
|
|||||||
// "application/json" --> JSON binding
|
// "application/json" --> JSON binding
|
||||||
// "application/xml" --> XML binding
|
// "application/xml" --> XML binding
|
||||||
//
|
//
|
||||||
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
|
// It parses the request's body based on the Content-Type (e.g., JSON or XML).
|
||||||
// It decodes the json payload into the struct specified as a pointer.
|
// It decodes the payload into the struct specified as a pointer.
|
||||||
// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
|
// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
|
||||||
func (c *Context) Bind(obj any) error {
|
func (c *Context) Bind(obj any) error {
|
||||||
b := binding.Default(c.Request.Method, c.ContentType())
|
b := binding.Default(c.Request.Method, c.ContentType())
|
||||||
@ -832,8 +832,8 @@ func (c *Context) MustBindWith(obj any, b binding.Binding) error {
|
|||||||
// "application/json" --> JSON binding
|
// "application/json" --> JSON binding
|
||||||
// "application/xml" --> XML binding
|
// "application/xml" --> XML binding
|
||||||
//
|
//
|
||||||
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
|
// It parses the request's body based on the Content-Type (e.g., JSON or XML).
|
||||||
// It decodes the json payload into the struct specified as a pointer.
|
// It decodes the payload into the struct specified as a pointer.
|
||||||
// Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid.
|
// Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid.
|
||||||
func (c *Context) ShouldBind(obj any) error {
|
func (c *Context) ShouldBind(obj any) error {
|
||||||
b := binding.Default(c.Request.Method, c.ContentType())
|
b := binding.Default(c.Request.Method, c.ContentType())
|
||||||
|
|||||||
@ -26,8 +26,6 @@ const (
|
|||||||
ErrorTypePublic ErrorType = 1 << 1
|
ErrorTypePublic ErrorType = 1 << 1
|
||||||
// ErrorTypeAny indicates any other error.
|
// ErrorTypeAny indicates any other error.
|
||||||
ErrorTypeAny ErrorType = 1<<64 - 1
|
ErrorTypeAny ErrorType = 1<<64 - 1
|
||||||
// ErrorTypeNu indicates any other error.
|
|
||||||
ErrorTypeNu = 2
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Error represents a error's specification.
|
// Error represents a error's specification.
|
||||||
|
|||||||
@ -22,7 +22,7 @@ func TestPanicClean(t *testing.T) {
|
|||||||
router.Use(RecoveryWithWriter(buffer))
|
router.Use(RecoveryWithWriter(buffer))
|
||||||
router.GET("/recovery", func(c *Context) {
|
router.GET("/recovery", func(c *Context) {
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oops, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(router, http.MethodGet, "/recovery",
|
w := PerformRequest(router, http.MethodGet, "/recovery",
|
||||||
@ -52,14 +52,14 @@ func TestPanicInHandler(t *testing.T) {
|
|||||||
router := New()
|
router := New()
|
||||||
router.Use(RecoveryWithWriter(buffer))
|
router.Use(RecoveryWithWriter(buffer))
|
||||||
router.GET("/recovery", func(_ *Context) {
|
router.GET("/recovery", func(_ *Context) {
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oops, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, buffer.String(), "panic recovered")
|
assert.Contains(t, buffer.String(), "panic recovered")
|
||||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")
|
||||||
assert.Contains(t, buffer.String(), t.Name())
|
assert.Contains(t, buffer.String(), t.Name())
|
||||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ func TestPanicWithAbort(t *testing.T) {
|
|||||||
router.Use(RecoveryWithWriter(nil))
|
router.Use(RecoveryWithWriter(nil))
|
||||||
router.GET("/recovery", func(c *Context) {
|
router.GET("/recovery", func(c *Context) {
|
||||||
c.AbortWithStatus(http.StatusBadRequest)
|
c.AbortWithStatus(http.StatusBadRequest)
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oops, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||||
@ -162,14 +162,14 @@ func TestCustomRecoveryWithWriter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
router.Use(CustomRecoveryWithWriter(buffer, handleRecovery))
|
router.Use(CustomRecoveryWithWriter(buffer, handleRecovery))
|
||||||
router.GET("/recovery", func(_ *Context) {
|
router.GET("/recovery", func(_ *Context) {
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oops, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
assert.Contains(t, buffer.String(), "panic recovered")
|
assert.Contains(t, buffer.String(), "panic recovered")
|
||||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")
|
||||||
assert.Contains(t, buffer.String(), t.Name())
|
assert.Contains(t, buffer.String(), t.Name())
|
||||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||||
|
|
||||||
@ -181,7 +181,7 @@ func TestCustomRecoveryWithWriter(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
assert.Contains(t, buffer.String(), "GET /recovery")
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
||||||
|
|
||||||
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())
|
||||||
|
|
||||||
SetMode(TestMode)
|
SetMode(TestMode)
|
||||||
}
|
}
|
||||||
@ -197,14 +197,14 @@ func TestCustomRecovery(t *testing.T) {
|
|||||||
}
|
}
|
||||||
router.Use(CustomRecovery(handleRecovery))
|
router.Use(CustomRecovery(handleRecovery))
|
||||||
router.GET("/recovery", func(_ *Context) {
|
router.GET("/recovery", func(_ *Context) {
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oops, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
assert.Contains(t, buffer.String(), "panic recovered")
|
assert.Contains(t, buffer.String(), "panic recovered")
|
||||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")
|
||||||
assert.Contains(t, buffer.String(), t.Name())
|
assert.Contains(t, buffer.String(), t.Name())
|
||||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||||
|
|
||||||
@ -216,7 +216,7 @@ func TestCustomRecovery(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
assert.Contains(t, buffer.String(), "GET /recovery")
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
||||||
|
|
||||||
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())
|
||||||
|
|
||||||
SetMode(TestMode)
|
SetMode(TestMode)
|
||||||
}
|
}
|
||||||
@ -232,14 +232,14 @@ func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) {
|
|||||||
}
|
}
|
||||||
router.Use(RecoveryWithWriter(DefaultErrorWriter, handleRecovery))
|
router.Use(RecoveryWithWriter(DefaultErrorWriter, handleRecovery))
|
||||||
router.GET("/recovery", func(_ *Context) {
|
router.GET("/recovery", func(_ *Context) {
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oops, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
// RUN
|
// RUN
|
||||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
assert.Contains(t, buffer.String(), "panic recovered")
|
assert.Contains(t, buffer.String(), "panic recovered")
|
||||||
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
assert.Contains(t, buffer.String(), "Oops, Houston, we have a problem")
|
||||||
assert.Contains(t, buffer.String(), t.Name())
|
assert.Contains(t, buffer.String(), t.Name())
|
||||||
assert.NotContains(t, buffer.String(), "GET /recovery")
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
||||||
|
|
||||||
@ -251,7 +251,7 @@ func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||||
assert.Contains(t, buffer.String(), "GET /recovery")
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
||||||
|
|
||||||
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
assert.Equal(t, strings.Repeat("Oops, Houston, we have a problem", 2), errBuffer.String())
|
||||||
|
|
||||||
SetMode(TestMode)
|
SetMode(TestMode)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -169,7 +169,7 @@ func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// StaticFileFS works just like `StaticFile` but a custom `http.FileSystem` can be used instead..
|
// StaticFileFS works just like `StaticFile` but a custom `http.FileSystem` can be used instead.
|
||||||
// router.StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})
|
// router.StaticFileFS("favicon.ico", "./resources/favicon.ico", Dir{".", false})
|
||||||
// Gin by default uses: gin.Dir()
|
// Gin by default uses: gin.Dir()
|
||||||
func (group *RouterGroup) StaticFileFS(relativePath, filepath string, fs http.FileSystem) IRoutes {
|
func (group *RouterGroup) StaticFileFS(relativePath, filepath string, fs http.FileSystem) IRoutes {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user