{"record":{"id":"d954c05ba7611ef0","repo":"apache/maven","slug":"unable-to-write-settings","errorCode":null,"errorMessage":"Unable to write settings: {}","messagePattern":"Unable to write settings: (.+?)","errorType":"exception","errorClass":"XmlWriterException","httpStatus":null,"severity":"error","filePath":"impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSettingsXmlFactory.java","lineNumber":100,"sourceCode":"        }\n        try {\n            SettingsStaxWriter xmlWriter = new SettingsStaxWriter();\n            xmlWriter.setAddLocationInformation(false);\n\n            Function<Object, String> formatter = request.getInputLocationFormatter();\n            if (formatter != null) {\n                xmlWriter.setAddLocationInformation(true);\n                Function<InputLocation, String> adapter = formatter::apply;\n                xmlWriter.setStringFormatter(adapter);\n            }\n\n            if (writer != null) {\n                xmlWriter.write(writer, content);\n            } else {\n                xmlWriter.write(outputStream, content);\n            }\n        } catch (Exception e) {\n            throw new XmlWriterException(\"Unable to write settings: \" + getMessage(e), getLocation(e), e);\n        }\n    }\n\n    static <T> T nonNull(T t, String name) {\n        if (t == null) {\n            throw new IllegalArgumentException(name + \" cannot be null\");\n        }\n        return t;\n    }\n}\n","sourceCodeStart":82,"sourceCodeEnd":111,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSettingsXmlFactory.java#L82-L111","documentation":"Wrapper thrown by DefaultSettingsXmlFactory.write when serializing a Settings document fails: STaX writer errors, IOException on the target stream, or failures thrown by the configured InputLocationFormatter. The cause is preserved and its message appended.","triggerScenarios":"Output stream already closed or read-only; an InputLocationFormatter that throws on null locations; STaX implementation failures during write.","commonSituations":"Writing settings back to a read-only file; custom location formatters used for round-tripping settings with comments.","solutions":["Unwrap getCause() to identify the stream/IO versus formatter versus STaX failure","Verify the stream is open and writable before the call","Test the InputLocationFormatter separately if one is configured"],"exampleFix":"// before\ntry {\n    settingsXmlFactory.write(req);\n} catch (XmlWriterException e) {\n    throw new RuntimeException(e); // real cause hidden\n}\n\n// after\ntry {\n    settingsXmlFactory.write(req);\n} catch (XmlWriterException e) {\n    log.error(\"settings write failed: {}\", e.getCause().getMessage(), e.getCause());\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    settingsXmlFactory.write(req);\n} catch (XmlWriterException e) {\n    Throwable cause = e.getCause();\n    // distinguish stream failure vs formatter failure before deciding to retry\n    log.error(\"settings write failed\", cause);\n}","preventionTips":["Check that the output stream is open and writable right before the write","Unit-test InputLocationFormatter implementations against null locations"],"tags":["maven","settings","xml","io","serialization"],"backgroundTag":"xml-write-failed","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}