{"record":{"id":"93eea2167977fd9c","repo":"Anuken/Mindustry","slug":"error-writing-region","errorCode":null,"errorMessage":"Error writing region \"{}\".","messagePattern":"Error writing region \"(.+?)\"\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"core/src/mindustry/io/SaveFileReader.java","lineNumber":105,"sourceCode":"    public void readRegion(String name, DataInput stream, CounterInputStream counter, IORunner<DataInput> cons) throws IOException{\n        counter.resetCount();\n        int length;\n        try{\n            length = readChunk(stream, (chunkStream, len) -> cons.accept(chunkStream));\n        }catch(Throwable e){\n            throw new IOException(\"Error reading region \\\"\" + name + \"\\\".\", e);\n        }\n\n        if(length != counter.count - 4){\n            throw new IOException(\"Error reading region \\\"\" + name + \"\\\": read length mismatch. Expected: \" + length + \"; Actual: \" + (counter.count - 4));\n        }\n    }\n\n    public void writeRegion(String name, DataOutput stream, IORunner<DataOutput> cons) throws IOException{\n        try{\n            writeChunk(stream, writes -> cons.accept(writes.output));\n        }catch(Throwable e){\n            throw new IOException(\"Error writing region \\\"\" + name + \"\\\".\", e);\n        }\n    }\n\n    /** Write a chunk of input to the stream. An integer of some length is written first, followed by the data. */\n    public void writeChunk(DataOutput output, IORunner<Writes> runner) throws IOException{\n        boolean wasNested = chunkNested;\n\n        chunkNested = true;\n\n        //regions can be nested once, so use a different output if it's already nested\n        ReusableByteOutStream dout = wasNested ? byteOutput2 : byteOutput;\n\n        try{\n            //reset output position\n            dout.reset();\n            //write the needed info\n            runner.accept(wasNested ? writes2 : writes1);\n","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/Anuken/Mindustry/blob/f695ad7e60323ebced984fa26d0bcf0bc54296b4/core/src/mindustry/io/SaveFileReader.java#L87-L123","documentation":"Thrown by SaveFileReader.writeRegion when any Throwable escapes writeChunk while serializing a named save region. writeRegion wraps the chunk write in try/catch and rethrows an IOException tagged with the region name, chaining the cause. This is the write-side counterpart of the read-region error and indicates serialization or IO failure during saving.","triggerScenarios":"Any exception during serialization of a save region (e.g. an object failing to serialize, an IO error writing to the underlying stream, disk full). Bubbles up wrapped as 'Error writing region \"<name>\".'.","commonSituations":"Disk full or permission denied while saving; a serializable object throws during write (e.g. null where a primitive expected); a nested writeChunk fails; network save target drops.","solutions":["Inspect IOException.getCause() for the underlying failure (disk space, permission, NPE).","Free disk space / fix write permissions on the save directory.","If a serialization bug, ensure all written objects implement the expected write path and never write nulls to primitive outputs.","Catch IOException at the save boundary so a failed save doesn't crash the game; keep the previous save intact (write to temp, then rename)."],"exampleFix":"// before\nSaveIO.write(file);\n\n// after: write to temp then atomically rename, handle failure\ntry{\n    SaveIO.write(tempFile);\n    tempFile.moveTo(file);\n}catch(IOException e){\n    Log.err(\"Save failed writing region\", e.getCause());\n    ui.showErrorMessage(\"Failed to save.\");\n}","handlingStrategy":"try-catch","validationCode":"// pre-check writable target before serializing\nif(!file.parent().exists() && !file.parent().mkdirs()){\n    throw new IOException(\"Cannot create save directory\");\n}\nif(file.parent().freeSpace() < minRequiredBytes){\n    throw new IOException(\"Insufficient disk space for save\");\n}","typeGuard":null,"tryCatchPattern":"try{ SaveIO.write(file); }catch(IOException e){ Log.err(\"Failed writing region\", e.getCause()); ui.showErrorMessage(\"Save failed\"); }","preventionTips":["Write saves to a temp file then atomically rename, so a failed write leaves the prior save intact.","Check disk space and write permissions before saving.","Ensure all serialized objects are non-null where primitives are written.","Catch IOException at the save boundary; never let it crash the game."],"tags":["save","io","write","serialization"],"backgroundTag":null,"analyzedSha":"f695ad7e60323ebced984fa26d0bcf0bc54296b4","analyzedAt":"2026-08-14T04:31:16.262Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}