2015-11-16 11:21:57 -08:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
using System;
|
2015-10-15 15:09:37 -07:00
|
|
|
using Microsoft.Extensions.JsonParser.Sources;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
2015-11-27 16:19:54 -08:00
|
|
|
namespace Microsoft.DotNet.ProjectModel
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
public sealed class FileFormatException : Exception
|
|
|
|
{
|
|
|
|
private FileFormatException(string message) :
|
|
|
|
base(message)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private FileFormatException(string message, Exception innerException) :
|
|
|
|
base(message, innerException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public string Path { get; private set; }
|
|
|
|
public int Line { get; private set; }
|
|
|
|
public int Column { get; private set; }
|
|
|
|
|
|
|
|
public override string ToString()
|
2016-03-01 15:18:00 -08:00
|
|
|
{
|
|
|
|
return $"{Path}({Line},{Column}): Error: {base.ToString()}";
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal static FileFormatException Create(Exception exception, string filePath)
|
|
|
|
{
|
2015-10-15 15:09:37 -07:00
|
|
|
if (exception is JsonDeserializerException)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
return new FileFormatException(exception.Message, exception)
|
|
|
|
.WithFilePath(filePath)
|
2015-10-15 15:09:37 -07:00
|
|
|
.WithLineInfo((JsonDeserializerException)exception);
|
2015-10-13 14:31:29 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return new FileFormatException(exception.Message, exception)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(Exception exception, JsonValue jsonValue, string filePath)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = Create(exception, jsonValue)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(Exception exception, JsonValue jsonValue)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = new FileFormatException(exception.Message, exception)
|
|
|
|
.WithLineInfo(jsonValue);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(string message, JsonValue jsonValue, string filePath)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = Create(message, jsonValue)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static FileFormatException Create(string message, string filePath)
|
|
|
|
{
|
|
|
|
var result = new FileFormatException(message)
|
|
|
|
.WithFilePath(filePath);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
internal static FileFormatException Create(string message, JsonValue jsonValue)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
var result = new FileFormatException(message)
|
|
|
|
.WithLineInfo(jsonValue);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal FileFormatException WithFilePath(string path)
|
|
|
|
{
|
|
|
|
if (path == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
Path = path;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
private FileFormatException WithLineInfo(JsonValue value)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
if (value == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(value));
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
Line = value.Line;
|
|
|
|
Column = value.Column;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
private FileFormatException WithLineInfo(JsonDeserializerException exception)
|
2015-10-13 14:31:29 -07:00
|
|
|
{
|
|
|
|
if (exception == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(exception));
|
|
|
|
}
|
|
|
|
|
2015-10-15 15:09:37 -07:00
|
|
|
Line = exception.Line;
|
|
|
|
Column = exception.Column;
|
2015-10-13 14:31:29 -07:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 15:09:37 -07:00
|
|
|
}
|