{"record":{"id":"dbd0e3221da390f8","repo":"spring-projects/spring-security","slug":"an-error-occurred-writing-the-oauth-2-0-error-e","errorCode":null,"errorMessage":"An error occurred writing the OAuth 2.0 Error: ${ex.getMessage()}","messagePattern":"An error occurred writing the OAuth 2\\.0 Error: (.+?)","errorType":"http","errorClass":"HttpMessageNotWritableException","httpStatus":500,"severity":"error","filePath":"oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverter.java","lineNumber":101,"sourceCode":"\t\t\t\t.stream()\n\t\t\t\t.collect(Collectors.toMap(Map.Entry::getKey, (entry) -> String.valueOf(entry.getValue()))));\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tthrow new HttpMessageNotReadableException(\n\t\t\t\t\t\"An error occurred reading the OAuth 2.0 Error: \" + ex.getMessage(), ex, inputMessage);\n\t\t}\n\t}\n\n\t@Override\n\tprotected void writeInternal(OAuth2Error oauth2Error, HttpOutputMessage outputMessage)\n\t\t\tthrows HttpMessageNotWritableException {\n\t\ttry {\n\t\t\tMap<String, String> errorParameters = this.errorParametersConverter.convert(oauth2Error);\n\t\t\tthis.jsonMessageConverter.write(errorParameters, STRING_OBJECT_MAP.getType(), MediaType.APPLICATION_JSON,\n\t\t\t\t\toutputMessage);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tthrow new HttpMessageNotWritableException(\n\t\t\t\t\t\"An error occurred writing the OAuth 2.0 Error: \" + ex.getMessage(), ex);\n\t\t}\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the OAuth 2.0 Error parameters to an\n\t * {@link OAuth2Error}.\n\t * @param errorConverter the {@link Converter} used for converting to an\n\t * {@link OAuth2Error}\n\t */\n\tpublic final void setErrorConverter(Converter<Map<String, String>, OAuth2Error> errorConverter) {\n\t\tAssert.notNull(errorConverter, \"errorConverter cannot be null\");\n\t\tthis.errorConverter = errorConverter;\n\t}\n\n\t/**\n\t * Sets the {@link Converter} used for converting the {@link OAuth2Error} to a\n\t * {@code Map} representation of the OAuth 2.0 Error parameters.","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/spring-projects/spring-security/blob/96852e8860138a482cb13d1479573f24ff6443c6/oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/http/converter/OAuth2ErrorHttpMessageConverter.java#L83-L119","documentation":"This HttpMessageNotWritableException is thrown by OAuth2ErrorHttpMessageConverter.writeInternal when the OAuth2Error cannot be converted to a parameter map and written as JSON. It wraps exceptions from errorParametersConverter.convert or jsonMessageConverter.write.","triggerScenarios":"Calling writeInternal/write on an OAuth2ErrorHttpMessageConverter when the error-to-parameters converter throws or JSON serialization fails (no Jackson converter, broken output stream).","commonSituations":"Missing Jackson dependency, a custom errorParametersConverter that rejects an OAuth2Error with null errorCode, or an IOException while writing the body to a disconnected client.","solutions":["Check ex.getCause() for the concrete failure.","Ensure Jackson is available and a MappingJackson2HttpMessageConverter is set via setJsonMessageConverter.","If a custom errorParametersConverter is registered, verify it handles null description/errorUri fields.","Guard with try-catch and fall back to a plain 400 response body if serialization fails."],"exampleFix":"// before\nconverter.write(oauth2Error, MediaType.APPLICATION_JSON, outputMessage);\n// after\ntry {\n    converter.write(oauth2Error, MediaType.APPLICATION_JSON, outputMessage);\n} catch (HttpMessageNotWritableException ex) {\n    logger.error(\"OAuth2 error serialization failed\", ex.getCause());\n}","handlingStrategy":"try-catch","validationCode":"if (oauth2Error.getErrorCode() == null) {\n    throw new IllegalArgumentException(\"errorCode is required to serialize OAuth2Error\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    converter.write(oauth2Error, MediaType.APPLICATION_JSON, outputMessage);\n} catch (HttpMessageNotWritableException ex) {\n    logger.error(\"OAuth2 error write failed\", ex.getCause());\n    outputMessage.getHeaders().setContentType(MediaType.TEXT_PLAIN);\n}","preventionTips":["Always construct OAuth2Error with a non-null errorCode.","Ensure Jackson is present for the JSON converter.","Test serialization of errors with null errorDescription/errorUri."],"tags":["oauth2","http-message-conversion","json","spring-security"],"backgroundTag":"json-serialization-failed","analyzedSha":"96852e8860138a482cb13d1479573f24ff6443c6","analyzedAt":"2026-09-10T23:25:23.477Z","contentChangedAt":"2026-09-10T23:25:23.477Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}