blob: 0dba5b5f7af60d343302de0ca3c8961c561d5fef [file] [log] [blame]
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <errno.h>
#include "bin/utils.h"
static void FormatMessageIntoBuffer(DWORD code,
char* buffer,
int buffer_length) {
DWORD message_size =
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buffer,
buffer_length,
NULL);
if (message_size == 0) {
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
fprintf(stderr, "FormatMessage failed %d\n", GetLastError());
}
snprintf(buffer, buffer_length, "OS Error %d", code);
}
buffer[buffer_length - 1] = '\0';
}
OSError::OSError() : sub_system_(kSystem), code_(0), message_(NULL) {
set_code(GetLastError());
static const int kMaxMessageLength = 256;
char message[kMaxMessageLength];
FormatMessageIntoBuffer(code_, message, kMaxMessageLength);
SetMessage(message);
}
void OSError::SetCodeAndMessage(SubSystem sub_system, int code) {
set_sub_system(sub_system);
set_code(code);
static const int kMaxMessageLength = 256;
char message[kMaxMessageLength];
FormatMessageIntoBuffer(code_, message, kMaxMessageLength);
SetMessage(message);
}