gofr-dev/gofr · error
incorrect file type
Error message
incorrect file type
What it means
errIncorrectFileType is the sentinel returned by (*FileSystem).Rename when the old and new names are not of the same type — specifically when a file is renamed with a new name whose extension differs from the old one. S3 has no real rename, so the implementation copies + deletes, and it refuses cross-type renames rather than guessing intent.
Source
Thrown at pkg/gofr/datasource/file/s3/fs.go:26
"mime"
"os"
"path"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
file "gofr.dev/pkg/gofr/datasource/file"
)
const (
typeFile = "file"
typeDirectory = "directory"
)
var (
errIncorrectFileType = errors.New("incorrect file type")
)
// client struct embeds the *s3.Client.
type client struct {
*s3.Client
}
type FileSystem struct {
s3File S3File
conn s3Client
presigner s3Presigner
config *Config
logger Logger
metrics Metrics
}
// Config represents the s3 configuration.
type Config struct {View on GitHub (pinned to 187eb24962)
Solutions
- Keep the same extension in the new name: strip only the base name portion.
- If a format/type change is really intended, copy the object to the new key with the new extension via your own CopyObject call instead of Rename.
- If the extension is semantically meaningless in your bucket layout, name files without extensions consistently (note: an empty extension is treated as a directory here).
Example fix
// before
fs.Rename("bucket/report.txt", "bucket/report-final.csv") // error
// after
fs.Rename("bucket/report.txt", "bucket/report-final.txt") Defensive patterns
Strategy: validation
Validate before calling
func canRename(old, new string) bool {
return path.Ext(old) == path.Ext(new)
}
// guard:
if !canRename(oldName, newName) { /* fix newName before calling Rename */ } Try / catch
if err := fs.Rename(old, new); err != nil {
if strings.Contains(err.Error(), "incorrect file type") {
// keep the original extension and retry
}
return err
} Prevention
- Build new names by replacing only the base name, never the extension.
- Treat empty extension as directory in this API — don't mix file/dir renames.
- Do real format conversion with copy+transform, not Rename.
- Validate extension equality before calling Rename.
When it happens
Trigger: Calling Rename(oldname, newname) where path.Ext(oldname) != path.Ext(newname) and the old name is a file (non-empty extension), e.g. Rename("bucket/a.txt", "bucket/a.json").
Common situations: Renaming a file and accidentally changing its extension; code that rewrites names with a different suffix; converting formats via 'rename' instead of copy+transform.
Related errors
- %w: negative offset %d
- out of range
- %w: renaming as well as moving file to different location is
- %w: new filename must match the old file's type
- operation not permitted
AI-assisted analysis of gofr-dev/gofr@187eb24962 (2026-09-01).
Data as JSON: /api/errors/6fcc76420380bb52.
Report an issue: GitHub.